출처 : 백준, https://www.acmicpc.net/problem/16236 16236번: 아기 상어 N×N 크기의 공간에 물고기 M마리와 아기 상어 1마리가 있다. 공간은 1×1 크기의 정사각형 칸으로 나누어져 있다. 한 칸에는 물고기가 최대 1마리 존재한다. 아기 상어와 물고기는 모두 크기를 가 www.acmicpc.net 더보기 먼저, 생각해야 할 부분 1. 먹을 수 있는 물고기의 위치와 거기까지의 거리를 어떻게 찾을지 생각해야한다. 2. 같은 거리의 물고기 중 위 왼쪽에 위치한 물고기를 찾아야한다. 풀이 import sys from collections import deque KEY_SIZE = 'size' KEY_EAT_FISH = 'eatFish' KEY_POINT = 'point' d..
출처: 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, 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, 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, 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, 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, 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, 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, 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, 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, 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..