How TypeScript Added Conditional Types
Daniel Rosenwasser, TypeScript's program manager, joined me to discuss the process for adding conditional types and other new type-level features to the language.
According to Daniel, most of the big additions like conditional types are motivated by conversations with other developers in order to u
Transcript
0:00 I spoke to TypeScript's program manager, Daniel Rosenwasser, and asked him what their criteria was for adding new type-level stuff to the language, especially about conditional types.
0:10 I would say the big ones, like you just mentioned, conditional types, those were often motivated from us talking to other developers and trying to understand what their use cases were.
0:23 Conditional types, I remember coming back from Microsoft's Build conference. We had this entire conversation about laughs...It was Knockout actually. We were talking about, "I want to be able to express patterns of what Knockout does with their observable model."
0:42 With Knockout's framework, you set a thing. Some other stuff gets updated automagically. Then you wrap an array. It becomes a reactive array. You wrap an object. It becomes a reactive object.
0:53 A primitive doesn't get mapped, is the whole idea. You need a way to say, "I'm going to wrap this thing if it satisfies some set of types." That was conditional types. Give me a way to be able to say, "If it's an array, it becomes a reactive array of whatever you had. Leave it alone otherwise." That was the core idea of conditionals.
1:19 That took a little bit of iteration because we were deciding, "Is it a match type? Or do we just go with a choice of two, and you can chain them with conditional type?"
1:29 There we go. Because of Knockout.js, amazingly, this is how we end up with conditional types in TypeScript. Because, in Knockout, they needed to perform if-else logic on the type level to work out whether to wrap an array or whether to not wrap a primitive. Amazing.