26
loading...
This website collects cookies to deliver better user experience
Use interface until you need type
—orta
Feature | I | T | Example |
---|---|---|---|
Primitives | ❌ | ✅ | type UUID = string |
Extend / Intersect | ✅ | ✅ | Response & ErrorHandling |
Unions | ❌ | ✅ | string | number |
Mapped object types | ❌ | ✅ | ['apple' | 'orange']: number |
Augment existing types | ✅ | ❌ | declare global { interface Window { … } } |
Declare type with typeof | ❌ | ✅ | Response = typeof ReturnType<fetch> |
Tuples | ✅ | ✅ | [string, number] |
Functions | ✅ | ✅ | (x: number, y: number): void |
Recursion | ✅ | ✅ | Nested { children?: Nested[] } |
string
, number
and boolean
make up the Primitives in Typescript.never
[key: string]
: only strings as key allowed[key: number]
: only numbers as key allowed[key in keyof T as `get${Capitalize<string & key>}`]
: only allow keys that start with get...
, e.g. as seen in a Getter objectOR
: The type is either x
or y
or z
or as many as you want.jQuery
library for auto completion) onto an existing type (e.g. window
).const [name, setName] = useState('')
?
to the recursive property. Otherwise the TS compiler spits out an error upon searching an endless recursion.