[TS] 조건부 타입과 infer를 활용한 타입 추론 연습
TIL
1. 조건을 만족하는 IsProductKey 타입 완성IsProductKey 타입- T가 Product의 key 중 하나일 경우 결과는 true 타입- T가 Product의 key 중 하나가 아닐 경우 결과는 false 타입interface Product { id: number; name: string; price: number; seller: { id: number; name: string; company: string; };}type IsProductKey = any;​type IsProductKey = T extends keyof Product ? true: false; 2. 조건을 만족하는 Extract 타입 구현Extract- T로부터 U만 추출하는 타입type Extr..
[TS] keyof와 typeof 연산자
TIL
keyof 연산자keyof 연산자는 객체 타입의 모든 프로퍼티 키를 String Literal Union 타입으로 추출하는 연산자이다. 객체의 프로퍼티 값을 가져오는 함수를 작성해보자.interface Person { name: string; hp: number;}function getPropertyKey(person: Person, key: "name" | "hp") { return person[key];}const person: Person = { name: "woodstock", hp: 90,};getPropertyKey(person, "name");이렇게 key의 타입을 "name" | "hp"로 정의하면 Person 타입에 프로퍼티가 추가되거나 수정될 때마다 이 타입도 함께 바꿔야 한다..