-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ts - infer, Required, Record, NonNullable
- Loading branch information
Showing
2 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
### Required, Record, NonNullable | ||
|
||
#### Required<T> | ||
```ts | ||
interface Profile { | ||
name: string | ||
age: number | ||
married: boolean | ||
} | ||
|
||
|
||
const someone: Required<Profile> = { | ||
name: 'someone', | ||
age: 30, | ||
married: false | ||
} | ||
|
||
type R<T> = { | ||
[key in keyof T]-?: T[key] | ||
} | ||
``` | ||
- ```-?``` Opitonal์ ```-``` Optional์ ์ ๊ฑฐํ๋๊ฒ | ||
#### Readonly<T> | ||
```ts | ||
interface Profile { | ||
name: string | ||
age: number | ||
married: boolean | ||
} | ||
|
||
|
||
const someone: R<Profile, 'married'> = { | ||
name: 'someone', | ||
age: 30, | ||
married: false | ||
} | ||
|
||
type R<T> = { | ||
readonly [key in keyof T]: T[key] | ||
// -readonly [key in keyof T]: T[key] | ||
// readonly๋ฅผ ์ ๊ฑฐํ๋๊ฒ | ||
} | ||
``` | ||
#### Record<T> | ||
```ts | ||
interface Obj { | ||
[key: string]: number | ||
} | ||
const a: Obj = { a: 3, b: 5, c: 7}; | ||
const a: Record<string, number> = { a: 3, b: 5, c: 7}; | ||
|
||
type _Record<T extends keyof any, S> = { | ||
[key in T]: S | ||
} | ||
// <T extends keyof any> | ||
// ๊ฐ ๋ถ๋ ์ด์ ๋ ๊ฐ์ฒด์ ํค๊ฐ string, number, symbol๋ง ์์ผ ํ๊ธฐ ๋๋ฌธ์ | ||
``` | ||
#### NonNullable | ||
```ts | ||
type A = string | null | undefined | boolean | number; | ||
type B = NonNullable<A> | ||
// B: string | boolean | number | ||
|
||
type _NonNullable<T> = T extends null | undefined ? never : T; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
### infer ํ์ ๋ถ์ | ||
|
||
```ts | ||
function zip(x: number, y: string, z: boolean): { x: number, y: string, z: boolean } { | ||
return { x , y, z }; | ||
} | ||
|
||
type Params = Parameters<typeof zip> | ||
// Params: [x: number, y: string, z: boolean] | ||
type Ret = ReturnType<typeof zip> | ||
// Ret: [x: number, y: string, z: boolean] | ||
type First = Params[0]; | ||
// First: number | ||
|
||
type _Parameters<T extends (...args: any) => any> = T extends (...args: infer A) => any ? A: never; | ||
``` | ||
|
||
- _Parameters์ ์ ๋ค๋ฆญ ```<T extends (...args: any) => any>``` ๋ T๋ฅผ ํจ์๋ก ์ ํํ๋ ์๋ฏธ์ด๋ฉฐ | ||
- infer A: T๋ ํจ์์ฌ์ผํ๊ณ ๊ทธ ํ๋ผ๋ฏธํฐ๋ค์ A๋ผ๊ณ ํ๋ค. ์ด๋ฅผ ๋ค์ ์ผํญ์ฐ์ฐ์์์ ์ฌ์ฉํ ๊ฒ์ด๋ค ๋ผ๋ ์๋ฏธ์ด๋ค. | ||
|
||
```ts | ||
class Profile { | ||
name: string; | ||
age: number; | ||
gender: 'male' | 'female'; | ||
constructor(name: string, age: number, gender: 'male' | 'female') { | ||
this.name = name; | ||
this.age = age; | ||
this.gender = gender; | ||
} | ||
} | ||
|
||
const p = new Profile('test', 123, 'male'); | ||
type P = ConstructorParameters<typeof Profile>; | ||
type I = InstanceType<typeof Profile>; | ||
``` |