Functions, generics and narrowing
This part is about functions and the types they carry: the signature, optional, default and rest parameters, overloads, generic functions and their constraints, explicit return types, arrows and callbacks. It closes with narrowing a union through typeof, in and instanceof, and with the assertions that can lie to the compiler.
3.1
Function types
type a signature with parameter and return annotations, including void and never
Open
3.2
Parameters
optional, default and rest parameters, destructured with full type safety
Open
3.3
Overloads
several call shapes under one name, each with a signature, and one implementation behind them
Open
3.4
Generics
generic functions with a type parameter, inferred from the call or given by hand
Open
3.5
Constraints
limit a generic with extends, derive keys with keyof, and default a type parameter
Open
3.6
Return types
explicit return types against inference, and the promise an async function returns
Open
3.7
Arrow functions
short arrow functions, the this they keep, and inline callbacks
Open
3.8
Callbacks
type a callback, pass a named or an inline handler, and flatten nested async logic
Open
3.9
Narrowing
narrow a union with typeof, truthiness, the in operator and instanceof
Open
3.10
Assertions
as, the non-null operator and const assertions, and where an assertion can lie
Open