반응형

Typescript 5

Type challenges - First of Array, Length of Tuple, Exclude

지난 글에 이어 Type challenges를 풀이한 내용을 올립니다. First of Array generic에 배열 타입을 넣으면 배열의 첫 번째 요소의 타입을 구현한다. type arr1 = ['a', 'b', 'c'] type arr2 = [3, 2, 1] type head1 = First // expected to be 'a' type head2 = First // expected to be 3 solutions type First = T extends [] ? never : T[0]; generic 타입으로 입력 된 타입을 배열로 제한한다. extends any[] (Generic Constraints) 빈 배열이 들어온 경우를 처리한다..

Typescript 2022.03.23

Typescript challenges - Readonly, Pick, Tuple of object

타입스크립트를 잘 써보고 싶기도 하고 재밌어 보여서 type-challenges를 시작했습니다. 이번 글에서는 type challenges easy 단계인 Pick, Readonly, Tuple of Object 과제를 풀어보고 정리해보았습니다 ~ Pick 구현하기 Pick interface Todo { title: string description: string completed: boolean } type TodoPrevidew = MyPick const todo: TodoPrivew = { title: 'Celan room', completed: false, } solution type MyPick = { [P in K]: T[P] } 어떤 타입을 받아오고, K라는 것은 T 타입에 있는 키들을 상속..

Typescript 2022.03.19

[Typescript] Utility Type이란?

타입스크립트에서는 다른 일반적인 프로그래밍 언어에서는 찾을 수 없는, 타입을 변환하는 것이 가능하다. 별 모양의 타입을 별 모양의 일부분만 변환하는 transform도 가능하다. utility 타입을 가져다가 쓸 수도 있지만, 정확하게 어떻게 이것이 가능한 것인지 먼저 알아본다. Index Type { const obj = { name: 'ellie' } obj.name; obj['name'] } 이것처럼 인덱스를 기준으로 타입을 결정할 수 있다. type Animal = { name: string; age: number; gender: 'male' | 'female' } type Name = Animal['name'] // stri..

Typescript 2021.09.16

[Typescript] Type vs Interface

이번 글에서는 Type과 Interface의 차이점을 정리했다. Type Alias vs Interface - 구현 모든 곳에 인터페이스를 쓰는 것은 좋지 않다. 타입과 인터페이스는 다르다. type PositionType = { x: number; y: number } interface PositionInterface { x: number; y: number; } const obj1: PositionType = { x: 1, y: 2 } const obj2: PositionInterface = { x: 1, y: 2 } class Pos1 implements PositionType { x: number y: number } class Pos2 implements PositionInterface { x:..

Typescript 2021.08.12

[Typescript] 기본 타입 알아보기

타입스크립트 기본 타입 이번 글에서는 타입스크립트 기본 타입을 정리했습니다. Array vs string 이 둘의 차이점은 readonly 변수를 만들 때 차이가 난다. function pushNumber(numbers: readonly number[]) { numbers.push(1) // warning (x) } function pushNumber(numbers: readonly Array) { numbers.push(1) // 'readonly' type modifier is only permitted on array and tuple literal types } Object의 불변성을 보장하기 하고 일관성 있는 코드를 작성하고 싶다면 string[]을 사용하는 것이 좋다. Tuple은 언제 사용할..

Typescript 2021.06.17