Loading topic
infer is an extension of the conditional type, and every recursive type you will write later is a conditional type that refers to itself. This chapter covers the syntax, what the test means, ho5 min readT extends U ? X : Y). You should also be comfortable reading a mapped type.6 min readT extends U ? X : Y). Earlier chapters covered how extends picks a branch and how conditionals distribute over unions. Here you learn the infer keyword, which lets a conditiona6 min readT extends U ? X : Y form with a first look at infer. It covers where infer can sit inside a pattern and what each position gives you.4 min readinfer U extends C form, which captures part of a type and also requires that part to satisfy a constraint. It assumes you know generics, tuple types, template literal types (basic syntax), and the plain infer keyword from the earlier chapters. The examples need TypeScript5 min readinfer to capture one piece of a type. This chapter covers what happens when the same infer variable is captured in several places, or from several union members. The result is sometimes a union and sometimes an intersection, and the position of each capture decides which.6 min readkeyof in a mapped type. It covers how a type alias can mention its own name, which self-references the compiler accepts, and which ones fail. The later chapters on recursive conditional types5 min readT extends U ? X : Y) and the infer keyword from the earlier chapters. Here you combine the two. A conditional type calls itself on the type it just extracted5 min readkeyof, conditional types with extends, and the basics of infer. It builds one utility, DeepReadonly, and uses it to show how a mapped type can refer to itself. It also covers the cases where a naive version breaks.5 min readinfer to capture a type, and write simple recursive conditional types with a base case. It also assumes you know basic template literal types such as ` ${A}-${B} `. Here you will combine these to take a string apart at the type level.6 min readinfer. It also assumes you know tuple types, rest elements in tuples ([A, ...B[]]), and mapped types. Earlier chapters covered recursive conditional types, such as unwrapping nested Promises. This chapter app6 min readinfer, and that you have seen Split, Reverse and UnwrapPromise from earlier chapters. Here the question is why such types sometimes fail. You will see how recursion ends, where the compiler stops it, and how to rewrite a ty6 min readReverse type that returns the wrong tuple compiles without complaint until some caller depends on it. This chapter shows how to turn a type-level result into a compile-time assertion, so tsc fails when a utility is wrong.5 min read