filmov
tv
TypeScript cheatsheet - here’s what you need to know to build better TypeScript applications:
Показать описание
- number: Holds numeric values, like let age: number = 30;
- string: For text data. E.g., let name: string = "Alice";
- boolean: Holds true or false. Useful for conditionals.
- any: Accepts any type, but it's best to avoid using this if you can help it.
- number[]: An array containing only numbers. E.g., let scores: number[] = [90, 85, 77];
- [number, string]: Tuple type, first element is a number, second is a string.
- enum Color {Red, Green, Blue}: Custom type with limited possible values.
- interface Person { name: string, age: number }: Define a custom shape for objects.
- class Car { drive() {} }: Define object structure and methods in a blueprint.
- function add(a: number, b: number): number { return a + b; }: Function with argument types and return type.
- number | string: Union type, can be either a number or a string.
- number & string: Intersection, mostly used with interfaces to combine multiple types into one.
- type ID = string | number: Create a new name for a type that could be one of several types.
- null: Represents no value or object for a variable.
- undefined: A variable that hasn't been assigned a value yet.
- name?: string: Optional parameter or property in an interface or function.
- readonly name: string: Value can't be modified after it's been initialized.
- import {...} from '...': Bring in functions or values from another module.
- export {...}: Make functions or values available for other modules to use.
- string: For text data. E.g., let name: string = "Alice";
- boolean: Holds true or false. Useful for conditionals.
- any: Accepts any type, but it's best to avoid using this if you can help it.
- number[]: An array containing only numbers. E.g., let scores: number[] = [90, 85, 77];
- [number, string]: Tuple type, first element is a number, second is a string.
- enum Color {Red, Green, Blue}: Custom type with limited possible values.
- interface Person { name: string, age: number }: Define a custom shape for objects.
- class Car { drive() {} }: Define object structure and methods in a blueprint.
- function add(a: number, b: number): number { return a + b; }: Function with argument types and return type.
- number | string: Union type, can be either a number or a string.
- number & string: Intersection, mostly used with interfaces to combine multiple types into one.
- type ID = string | number: Create a new name for a type that could be one of several types.
- null: Represents no value or object for a variable.
- undefined: A variable that hasn't been assigned a value yet.
- name?: string: Optional parameter or property in an interface or function.
- readonly name: string: Value can't be modified after it's been initialized.
- import {...} from '...': Bring in functions or values from another module.
- export {...}: Make functions or values available for other modules to use.