알고리즘/Type Challenges

알고리즘/Type Challenges

8 - Readonly 2

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 8 - Readonly 2 `T`에서 `K` 프로퍼티만 읽기 전용으로 설정해 새로운 오브젝트 타입을 만드는 제네릭 `MyReadonly2`를 구현하세요. `K`가 주어지지 않으면 단순히 `Readonly`처럼 모든 프로퍼티를 읽기 전용으로 설정해야 합니다. interface Todo { title: string description: string completed: boolean } const todo: MyReadonly2 = { title: "Hey", description: "foobar", completed: false, } to..

알고리즘/Type Challenges

3 - Omit

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 3 - Omit `T`에서 `K` 프로퍼티만 제거해 새로운 오브젝트 타입을 만드는 내장 제네릭 `Omit`를 이를 사용하지 않고 구현하세요. interface Todo { title: string description: string completed: boolean } type TodoPreview = MyOmit const todo: TodoPreview = { completed: false, } 풀이 type MyOmit = { [P in keyof T as P extends K ? never : P]: T[P] } 대부분은 Pick..

알고리즘/Type Challenges

2 - Get Return Type

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 2 - Get Return Type 내장 제네릭 `ReturnType`을 이를 사용하지 않고 구현하세요. const fn = (v: boolean) => { if (v) return 1 else return 2 } type a = MyReturnType // should be "1 | 2" 풀이 type MyReturnType = T extends (...args: any) => infer ReturnType ? ReturnType : never args는 :을 통해서 파라미터 타입을 지정해주는 반면에 리턴 타입은 바로 infer로 가져..

알고리즘/Type Challenges

898 - Includes

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 898 - Includes JavaScript의 `Array.includes` 함수를 타입 시스템에서 구현하세요. 타입은 두 인수를 받고, `true` 또는 `false`를 반환해야 합니다. type isPillarMen = Includes // expected to be `false` 풀이 type IsEqual = (() => T extends X ? 1 : 2) extends (() => T extends Y ? 1 : 2) ? true : false type Includes = T extends [infer First, ...in..

알고리즘/Type Challenges

3312 - Parameters

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 3312 - Parameters 내장 제네릭 ``Parameters``를 이를 사용하지 않고 구현하세요. const foo = (arg1: string, arg2: number): void => {} type FunctionParamsType = MyParameters // [arg1: string, arg2: number] 풀이 type MyParameters any> = T extends (...args: infer Params) => any ? Params : never

알고리즘/Type Challenges

3060 - Unshift

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 3060 - Unshift `Array.unshift`의 타입 버전을 구현하세요. type Result = Unshift // [0, 1, 2,] 풀이 type Unshift = T extends [...infer Rest] ? [U, ...Rest] : never

알고리즘/Type Challenges

3057 - Push

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 3057 - Push `Array.push`의 제네릭 버전을 구현하세요. type Result = Push // [1, 2, '3'] 풀이 type Push = T extends [...infer REST] ? [...REST, U] : never infer를 사용해서 ...를 사용해서 배열 내부를 그대로 가져와서 연결하면 된다.

알고리즘/Type Challenges

533 - Concat

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 533 - Concat JavaScript의 `Array.concat` 함수를 타입 시스템에서 구현하세요. 타입은 두 인수를 받고, 인수를 왼쪽부터 concat한 새로운 배열을 반환해야 합니다. type Result = Concat // expected to be [1, 2] 풀이 type Concat = T extends readonly [...infer A] ? U extends readonly [...infer B] ? [...A, ...B] : never : never; extends와 infer를 통해서 T와 U 내부의 타입들을..

알고리즘/Type Challenges

268 - If

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 268 - If 조건 `C`, 참일 때 반환하는 타입 `T`, 거짓일 때 반환하는 타입 `F`를 받는 타입 If를 구현하세요. `C`는 `true` 또는 `false`이고, `T`와 `F`는 아무 타입입니다. type A = If // expected to be 'a' type B = If // expected to be 'b' 풀이 type If = C extends true ? T : F; extends를 이용한 조건문 연습 문제인 듯 하다.

알고리즘/Type Challenges

189 - Awaited

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 189 - Awaited Promise와 같은 타입에 감싸인 타입이 있을 때, 안에 감싸인 타입이 무엇인지 어떻게 알 수 있을까요? type ExampleType = Promise type Result = MyAwaited // string 풀이 type Thenable = { then: (_: (_:T) => any) => any } type MyAwaited = T extends Thenable ? U extends Thenable ? MyAwaited : U : never type MyAwaited = T extends { then..

알고리즘/Type Challenges

43 - Exclude

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 43 - Exclude `T`에서 `U`에 할당할 수 있는 타입을 제외하는 내장 제네릭 `Exclude`를 이를 사용하지 않고 구현하세요. type Result = MyExclude // 'b' | 'c' 풀이 type MyExclude = T extends U ? never : T; 타입스크립트의 타입시스템은 유니온 타입을 분배시켜 처리한다. 예를 들어 `'a' | 'b' | 'c' extends 'a'`라면 이는 `'a' extends 'a' | 'b' extends 'a' | 'c' extends 'a'`로 처리된다. 따라서 `My..

알고리즘/Type Challenges

18 - Length of Tuple

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 18 - Length of Tuple 배열(튜플)을 받아 길이를 반환하는 제네릭 Length를 구현하세요. type tesla = ['tesla', 'model 3', 'model X', 'model Y'] type spaceX = ['FALCON 9', 'FALCON HEAVY', 'DRAGON', 'STARSHIP', 'HUMAN SPACEFLIGHT'] type teslaLength = Length // expected 4 type spaceXLength = Length // expected 5 풀이 type Length = T['..

제주도랏맨
'알고리즘/Type Challenges' 카테고리의 글 목록