TypeScript
https://www.typescriptlang.org/docs/handbook/intro.html
Key Concepts
- Unions:
|meaning a value can be one of several types (including undefined or null) - Generics: provide variables to types. Or, a type that can be used with multiple other types
Recommendations
- Prefer
interfaceovertype - Priority: mapped types over unions over enums
export const Animal = {
CAT: "CAT",
DOG: "DOG",
} as const;
export type Animal = (typeof Animal)[keyof typeof Animal];
or
export const Animals = {
CAT: "CAT",
DOG: "DOG",
} as const;
type Animals = typeof Animals
type Animal = Animals[keyof Animals];
// referencing in type definitions
Animals["CAT"]
// referencing in runtime code
Animals.CAT
May also have to turn off @typescript-eslint/no-redeclare
FAQs
- React children:
ReactNode - Objects with arbitrary keys: index signatures -
{ [key: string]: string }
Recommended tsconfig.json settings
{
"compilerOptions": {
"strict": true,
}
}
https://www.typescriptlang.org/tsconfig/#strict
Includes noImplicitAny and strictNullChecks
Slapdash