Update typescript.md (#1863)
This commit is contained in:
parent
fac4d1e60b
commit
6999e947cb
|
@ -18,16 +18,25 @@ string
|
||||||
null
|
null
|
||||||
undefined
|
undefined
|
||||||
|
|
||||||
|
bigint
|
||||||
|
symbol
|
||||||
|
|
||||||
string[] /* or Array<string> */
|
string[] /* or Array<string> */
|
||||||
[string, number] /* tuple */
|
[string, number] /* tuple */
|
||||||
|
|
||||||
string | null | undefined /* union */
|
string | null | undefined /* union */
|
||||||
|
|
||||||
never /* unreachable */
|
never /* unreachable */
|
||||||
|
unknown
|
||||||
```
|
```
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
enum Color {Red, Green, Blue = 4}
|
enum Color {
|
||||||
|
Red,
|
||||||
|
Green,
|
||||||
|
Blue = 4
|
||||||
|
};
|
||||||
|
|
||||||
let c: Color = Color.Green
|
let c: Color = Color.Green
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -97,8 +106,8 @@ function printLabel(options: LabelOptions) { ... }
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
interface User {
|
interface User {
|
||||||
name: string,
|
name: string;
|
||||||
age?: number
|
age?: number;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -124,6 +133,16 @@ interface User {
|
||||||
type Name = string | string[]
|
type Name = string | string[]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Intersection
|
||||||
|
|
||||||
|
```ts
|
||||||
|
interface Colorful { ... }
|
||||||
|
|
||||||
|
interface Circle { ... }
|
||||||
|
|
||||||
|
type ColorfulCircle = Colorful & Circle;
|
||||||
|
```
|
||||||
|
|
||||||
## Function types
|
## Function types
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
|
@ -204,10 +223,62 @@ export interface User { ... }
|
||||||
```ts
|
```ts
|
||||||
interface Building {
|
interface Building {
|
||||||
room: {
|
room: {
|
||||||
door: string,
|
door: string;
|
||||||
walls: string[],
|
walls: string[];
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
type Walls = Building['room']['walls']; // string[]
|
type Walls = Building['room']['walls']; // string[]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Keyof Type Operator
|
||||||
|
|
||||||
|
```ts
|
||||||
|
type Point = { x: number; y: number };
|
||||||
|
|
||||||
|
type P = keyof Point; // x | y
|
||||||
|
```
|
||||||
|
|
||||||
|
## Conditinal Types
|
||||||
|
|
||||||
|
```ts
|
||||||
|
// SomeType extends OtherType ? TrueType : FalseType;
|
||||||
|
|
||||||
|
type ToArray<T> = T extends any ? T[] : never;
|
||||||
|
|
||||||
|
type StrArrOrNumArr = ToArray<string | number>; // string[] | number[]
|
||||||
|
```
|
||||||
|
|
||||||
|
### Inferring
|
||||||
|
|
||||||
|
```ts
|
||||||
|
type GetReturnType<T> = T extends (...args: unknown[]) => infer R
|
||||||
|
? R
|
||||||
|
: never;
|
||||||
|
|
||||||
|
type Num = GetReturnType<() => number>; // number
|
||||||
|
```
|
||||||
|
|
||||||
|
```ts
|
||||||
|
type First<T extends Array<any>> = T extends [infer F, ...infer Rest] ? F : never;
|
||||||
|
|
||||||
|
type Str = First<['hello', 1, false]>; // 'hello'
|
||||||
|
```
|
||||||
|
|
||||||
|
## Literal Type
|
||||||
|
|
||||||
|
```ts
|
||||||
|
const point = { x: 4, y: 2 }; // { x: number, y: number }
|
||||||
|
|
||||||
|
const literalPoint = { x: 4, y: 2 } as const; // { readonly x: 4, readonly y: 2 };
|
||||||
|
```
|
||||||
|
|
||||||
|
## Template Literal Types
|
||||||
|
|
||||||
|
```ts
|
||||||
|
type SpaceChar = ' ' | '\n' | '\t';
|
||||||
|
|
||||||
|
type TrimLeft<S extends string> = S extends `${SpaceChar}${infer Rest}` ? TrimLeft<Rest> : S;
|
||||||
|
|
||||||
|
type Str = TrimLeft<' hello'>; // 'hello'
|
||||||
|
```
|
||||||
|
|
Loading…
Reference in New Issue