IT/typescript31 [Typescript] index signature vs mapped type 결론 mapped type 추천 이유 컴파일 시 에러가 발생하기 때문에 실수를 예방할 수 있음 type Fruite = 'Banana' | 'Apple' | 'Orange' const PriceMap: {[fruit:string]:number} = { Apple: 1000, Banana: 1000, Orange: 2000, } const PriceMap2: {[fruit in Fruite]:number} = { Apple: 1000, Banana: 1000, Orange: 2000, } function getDiscountedPrice(fruit: string, discount: number) { return PriceMap[fruit] - discount } const a = getDiscountedP.. 2024. 2. 5. [Typescript] enum vs union 결론 enum 보다는 union type 이유 컴파일 해보면 객체가 생성됨 > typescript 사용 이유가 정적 타입을 사용하기 위함인데, 정적 타입 정보가 런다음에 영향을 준다는 사실이 썩 내키지 않는다. > const enum을 사용해도 되지만, import 구문이 추가됨 enum Fruite { Banana, Apple, Orange } complied "use strict"; var Fruite; (function (Fruite) { Fruite[Fruite["Banana"] = 0] = "Banana"; Fruite[Fruite["Apple"] = 1] = "Apple"; Fruite[Fruite["Orange"] = 2] = "Orange"; })(Fruite || (Fruite = {}).. 2024. 2. 5. TS - 5.0 New features Decorators 실험적인 기능으로 제공되었었지만, ECMAScript (Stage 3) 스펙에 맞게 업데이트 됨 개념 class, method, accessor, property, parameter에 함수를 붙여 기능을 확장하는 함수 차이점 decorator 함수를 만들 때 기존과 다른 파라미터를 넘겨 받는다. 5.0으로 마이그레이션을 해야한다면 꼭 유의해야 한다. nestjs에서 TS 5.0을 지원하는지 확인해야겠죠? // v4.9.5 function loggedMethod(target: any, propertyKey: string, descriptor: PropertyDescriptor){ console.log("target: ", target); console.log("propertyKey: ".. 2023. 7. 1. TS - unknown vs any 개념 unknown, any 타입을 지정하기 애매할 때 사용 unknown 타입이 뭔지 모르겠으니 개발자가 타입을 정확히 지정주어야 한다. typeof, instanceof 등 타입을 확인하여 적절한 타입 주입시켜 사용한다. Example code reference: https://docs.nestjs.com/exception-filters#catch-everything @Catch() export class AllExceptionsFilter implements ExceptionFilter { constructor(private readonly httpAdapterHost: HttpAdapterHost) {} catch(exception: unknown, host: ArgumentsHost): void.. 2023. 6. 27. TS - never 개념 Bottom type을 나타내기 위해 사용하는 것 어떠한 값도 할당할 수 없다. Exhaustive Check (Bottom type) function foo(x: string | number): boolan { if (typeof x === 'string') { return true } else if (typeof x === 'number') { return false } return fail() } function fail(): never { throw new Error() } 어떠한 값도 할당할 수 없다. let a: never = null // Type 'null' is not assignable to type 'never'.(2322) let b: never = undefined // Ty.. 2023. 6. 27. TS - Extract<T, U> 개념 한마디로, T에서 U타입 중 할당 가능한 것만 리턴 정리하지면, pick은 prop을, extract는 type을... type Extract = T extends U ? T : never Examples type Company = { id: string name: string } type Product = { price: number rating: number } type fakeProduct = Extract // type fakeProduct = { // id: string; // name: string; // } 참고 https://velog.io/@ggong/Typescript의-유틸리티-타입-1-Record-Extract-Pick 2023. 6. 27. 이전 1 2 3 4 5 6 다음