-
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.
- Loading branch information
Showing
4 changed files
with
76 additions
and
2 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
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,17 @@ | ||
### filter 직접 만들기 | ||
|
||
```ts | ||
interface Arr<T> { | ||
filter<S extends T>(callback : (item: T) => v is S): S[]; | ||
} | ||
|
||
const a: Arr<number> = [1, 2, 3]; | ||
const b: a.filter((v) => v % 2 === 0); | ||
|
||
const c: Arr<number | string> = [1, '2', 3, '4', 5]; | ||
const d: c.filter((v): v is string => typeof v === 'string'); // d: string[] | ||
const e: c.filter((v): v is number => typeof v === 'number'); // e: number[] | ||
|
||
const predicate = (v: string | number): v is number => typeof v === 'number'; | ||
const e = c.filter(predicate); // e: number[] | ||
``` |
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,29 @@ | ||
### forEach 타입 직접 만들기 | ||
|
||
```ts | ||
interface Arr<T> { | ||
forEach(callback : (item: T) => void): void; | ||
// forEach에 제네릭 파라미터를 붙일 수도 있지만 그렇게한다면 | ||
// forEach 함수를 이용할때마다 제네릭 파라미터를 붙여줘야한다. | ||
// 그렇기때문에 이 방법보다는 interface에 제네릭을 이용하는 방법이 추론하기 더 유리하다 | ||
} | ||
const a: Arr<number> = [1,2,3]; | ||
a.forEach((item) => { | ||
console.log(item); | ||
item.toFixed(1); | ||
}) | ||
a.forEach((item) => { | ||
console.log(item); | ||
return '3'; | ||
}) | ||
|
||
const b: Arr<string> = ['1','2','3']; | ||
b.forEach((item) => { | ||
console.log(item); | ||
item.charAt(3); | ||
}) | ||
b.forEach((item) => { | ||
console.log(item); | ||
return '3'; | ||
}) | ||
``` |
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,28 @@ | ||
### map 타입 직접 만들기 | ||
|
||
```ts | ||
interface Arr<T> { | ||
map<S>(callback : (item: T) => S): S[]; | ||
} | ||
|
||
const a: Arr<number> = [1,2,3]; | ||
const b = a.map((v) => v + 1); | ||
const c = a.map((v) => v.toString()); // c: string[] | ||
const d = a.map((v) => v % 2 === 0); // d: boolean[] | ||
|
||
const e: Arr<string> = ['1', '2', '3']; | ||
const f = e.map((v) => +v); // f: number[] | ||
``` | ||
|
||
#### 생각해봐야 할것 | ||
```ts | ||
interface Arr<T, S> { | ||
map(callback : (item: T) => S): S[]; | ||
} | ||
|
||
const a: Arr<number, string> = [1,2,3]; | ||
const c = a.map((v) => v.toString()); | ||
``` | ||
- 이 예제에서는 interface를 선언시 제네릭을 두개 이용하는 방법으로 map 함수의 리턴 타입을 결정하는데 | ||
- 코드상으로 문제점은 없으나 변수 a의 선언시에 S에 해당하는 타입을 미리 넣어줘야 되야한다는 점이 단점으로 작용한다. | ||
- 배열의 선언시에 map의 이용여부와 관계없이, 그리고 어떻게 이용될지 모르는 배열에 관한 제네릭이 추가로 필요하다는 점이 말이안되기 때문에 이렇게 선언하면 안된다는 점을 알아두자. |