The introduction to TypeScript Generics you've been missing
swyx 2019-08-30
Note: this is an unfinished draft, but I’m sharing it anyway as an outline
by @swyx, with direct attribution and acknowledgement of @andrestaltz’s introduction to Reactive Programming
⚠️Note: this is a work in progress - I am not even a TypeScript expert, I am merely Learning In Public and jotting down non obvious, useful notes.
First thing to do is read the official Generics guide from the TS Handbook. I am not here to compete with the docs.
Based on this, I assume you know:
- where to place angle brackets
- especially in classes and interfaces
- etc from the intro
Structure
- Why you can’t avoid Generics
- Concepts
- Assignable (Compatibility)
- Constructor Generics
Promise<>
- Interface Modification Inbuilt Generics
- Partial
- Readonly
- Pick
- Omit
- Union Type Inbuilt Generics
- Exclude (parallel to Omit)
- Extract
- NonNullable
- Union Type AND Interface Modification Inbuilt Generics
- Record
- Extractor Inbuilt Generics
- ReturnType
- InstanceType
- ThisType (ignore)
- Required
- Know the Keywords
- readonly
- extends and implements
- keyof
- Mapped Types -> partial
- Resources You Might Miss
- TS Release notes https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html
- Basarat on Generics https://basarat.gitbooks.io/typescript/docs/types/generics.html
- 2ality https://2ality.com/2018/04/type-notation-typescript.html
- sharifsbeat https://dev.to/busypeoples/notes-on-typescript-pick-exclude-and-higher-order-components-40cp
- https://dev.to/busypeoples/-notes-on-typescript-react-and-generics-35c9
- testing types generics https://www.youtube.com/watch?v=nygcFEwOG8w&feature=share