알고리즘/Type Challenges

알고리즘/Type Challenges

14 - First of Array

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 14 - First of Array 배열(튜플) `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 풀이 type First = T extends [infer U, ...unknown] ? U : never `infer`는 `extends`와 함께 쓰이며 타입을 추출해서 뒤에서 다시 ..

알고리즘/Type Challenges

11 - Tuple to Object

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 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'} 풀이 type TupleToObject = { [P in ..

알고리즘/Type Challenges

7 - Readonly

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 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: ..

알고리즘/Type Challenges

4 - Pick

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 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 = { [Property in K]: T[Property]; } K가 T 타입의 key..

알고리즘/Type Challenges

13 - Hello World

출처: Type Challenges, 13 - Hello World 13 - Hello World Hello, World! Type Challenges에서는 타입 단언(assertion)을 하기 위해 자체적인 타입 시스템을 사용합니다. 이 과제에서는, 아래의 코드를 변경해서 테스트 코드를 통과하세요. (타입 체크 에러 없음). // string이 되어야 합니다. type HelloWorld = any // 아래의 테스트가 통과하도록 만드세요. type test = Expect 풀이 type HelloWorld = string

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