Conditional Types and Infer 10 exercises
explainer

Template Literal Types Were Nearly Regexes

I had a chat with Daniel Rosenwasser, TypeScript's program manager, who explained why template literal types are designed as they are, rather than using regexes.

The reason behind this is that people want to express specific requirements for strings, like strings of a certain length. Although regex

Loading explainer

Transcript

0:00 I spoke to TypeScript's program manager, Daniel Rosenwasser. He explained to me why template literal types are designed the way they are instead of maybe using something like regexes.

0:09 People wanted to be able to express like, "This only takes strings five characters long." We're like, "Regex types. What if you could have a type that was a regular expression and that just rejects any string that doesn't accept the regular expression?"

0:24 Except now you need some sort of mechanism for being able to capture from a regular expression. That doesn't make sense. You now need weird capture syntax that extends regular expressions.

0:39 I'm also extremely anti-regular expression for production programming. I don't believe anyone gets it right. We definitely get it wrong on our team. Everyone forgets that your regular expressions need to be anchored.

0:53 We had an implementation of regular expression types. I said, at a design meeting, "Effectively, I'm vetoing this. There's no way I'd feel comfortable with us doing this." Regular expressions are highly engine-dependent. If you just say, "Let the engine take care of this," then you have this entire other problem.

1:11 Template string types made me way more comfortable. Because now you're saying, "I want to support patterns, but I also want to be able to infer from parts of the pattern." I want to be able to say, "It starts with this string, ends with that string. Then, in the middle, I want to grab whatever's in there."

1:29 It composes really well with the rest of the type system. Yeah, you are creating a new language of pattern-matching, but at least you don't have to worry about some of the edge cases of regular expressions.

1:40 People would not write these correctly. One of my favorite things is that we said no to that. The team was so tempted. We were like, "No." It would be so easy. No.

1:52 The idea then is that you had these template literal types. They could compose with conditional types that you already had. You could use the infer syntax to just do capturing with them.

2:02 Exactly. Admittedly, this stuff gets hard to read in some cases. The whole idea is you look at what you have in your language primitives. You look at what people are really trying to accomplish. A lot of the time, people say, "I just need a regular expression. I just need this."

2:20 Conditional types allow you to capture types out of other types. Now you can capture types out of string literal types, to say like, "When you give me this string, try to match around it the same way that you do with other types. Then just give me whatever's inside of there in some way." People are building amazing things with it.

2:40 There you go. Template literal types were designed because of the way they work with conditional types. Were it not for Daniel's veto, we might have regex types in TypeScript today.