Loading topic
string | number is. It also assumes you know if, switch, and the ternary operator in JavaScript. It explains the mechanism behind all narrowing. Later chapters cover each spec6 min readtypeof guard. It is the first built-in narrowing construct and the one you will use most often for primitive values. It assumes you can read TypeScript union types such as string | number, and that you know if, switch and the ternary operator. The previous chapter exp6 min readstring | number, and JavaScript if statements, &&, ||, and the ternary operator. You should also have read the earlier chapters on how control flow analysis narrows a union inside a branch.6 min readstring | number, and JavaScript if statements. It also assumes you have seen the idea that TypeScript narrows a union inside a branch. Here you will see how equality checks do that narrowing, with a focus on null and `u5 min readin operator narrows a union by checking for a property. It assumes you can write TypeScript unions and type aliases, and that you know typeof and equality narrowing from earlier chapters. By the end you should know when in is the right tool, what it does to types th6 min readtypeof, equality, and in checks from the earlier chapters. It also assumes you are comfortable with TypeScript classes and extends, and that you know what a union type is.5 min readstring | number, and switch statements. It also assumes you have seen typeof, in, and equality checks narrow a value. Here you will learn how to design a union so the compiler narrows it with one cheap c5 min readswitch over a discriminated union. You should know that narrowing removes members from a union as checks rule them out. Here you will use that fact to make the compiler tell you when a union member has no handl5 min readstring | number, and JavaScript control flow (if, switch, the ternary operator). It also assumes you have seen the built-in narrowing checks (typeof, in, instanceof, equality)5 min readtypeof, in, and instanceof narrowing, and that you can write a type predicate (value is T). It builds on them to handle values whose shape you do not know at compile time. The examples need TypeScript 4.9 or later, and they assume strict mode.5 min readunknown, and type predicates (value is T) from the earlier chapters. An assertion function is a close relative of a type predicate. It narrows a value for all the code that follows the call, and it does so by throwing an error when the check fails.5 min readtypeof, !== null, and in checks, and that you have seen a user-defined type predicate of the form value is T. It also assumes you are comfortable with generics at the level of function f<T>(x: T). Here you will apply those tools to arrays, where the narro5 min readstring | number, and the narrowing checks from earlier chapters (typeof, equality, in, instanceof, discriminated unions, and type predicates). Here you will learn where the compiler stops trusting a narrowed type, why6 min read