Type Challenges

TypeScript/Type Challenges

[43] Exclude

문제 T에서 U에 할당할 수 있는 타입을 제외하는 내장 제네릭 Exclude를 이를 사용하지 않고 구현하세요. 예시 type Result = MyExclude // 'b' | 'c' 🔥 도전하기 TS Playground - An online editor for exploring TypeScript and JavaScript The Playground lets you write TypeScript or JavaScript online in a safe and sharable way. www.typescriptlang.org 풀이 해당 문제를 풀기 위해서는 분산 조건부 타입에 대해서 알아야 한다. T 가 유니온일 때 T extends U 와 같은 구조를 사용하면, TypeScript는 유니온 T 를 순회하면..

TypeScript/Type Challenges

[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 🔥 도전하기 TS Playground - An online editor for exploring TypeScript and JavaScript The Playground lets you write TypeScript or JavaSc..

TypeScript/Type Challenges

[14] easy first

문제 배열(듀플) T를 받아 첫 원소의 타입을 반환하는 제네릭 First를 구현하세요. 예시 type arr1 = ['a', 'b', 'c'] type arr2 = [3, 2, 1] type head1 = First // expected to be 'a' type head2 = First // expected to be 3 🔥 도전하기 TS Playground - An online editor for exploring TypeScript and JavaScript The Playground lets you write TypeScript or JavaScript online in a safe and sharable way. www.typescriptlang.org 풀이 해당 문제는 인덱스 접근 연산자를 사..

TypeScript/Type Challenges

[11] Tuple to Object

문제 배열(튜플)을 받아, 각 원소의 값을 key/value로 갖는 오브젝트 타입을 반환하는 타입을 구현하세요. 예시 const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as const type result = TupleToObject // expected { tesla: 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'} 풀이 배열에 있는 모든 값들을 얻어 새 객체의 키와 값으로 만들어야합니다. T[number](인덱스 타입)을 이용해서 기존의 원소를 키와 값으로 하는 새로운 타입을 만들 수 있습니다. type TupleToObject = {[K in T[number]] ..

TypeScript/Type Challenges

[7] Readonly

문제 T의 모든 프로퍼티를 읽기 전용(재할당 불가)으로 바꾸는 내장 제네릭 Readonly를 사용하지 않고 구현하세요. 예시 interface Todo { title: string; description: string; } const todo: MyReadonly = { title: "Hey", description: "foobar" } todo.title = "Hello" // Error: cannot reassign a readonly property todo.description = "barFoo" // Error: cannot reassign a readonly property 풀이 type MyReadonly = { readonly [key in keyof T]: T[key] } Readonly..

TypeScript/Type Challenges

[4] Pick

문제 T에서 K 프로퍼티만 선택해 새로운 오브젝트 타입을 만드는 내장 제네릭 Pick을 이를 사용하지 않고 구현하세요. 예시 interface Todo { title: string description: string completed: boolean } type TodoPreview = MyPick const todo: TodoPreview = { title: 'Clean room', completed: false, } 풀이 type MyPick = { [P in K]: T[P]; } Pick interface Todo { title: string; description: string; completed: boolean; } type TodoPreview = Pick; const todo: TodoPre..

후끈후끈
'Type Challenges' 태그의 글 목록