[TS] 타입 조작 및 고급 타입 활용 연습
TIL
1. 조건을 만족하는 함수 타입 구현getSellersFromProducts 함수- 매개변수로 받은 Product 배열로부터, seller 객체만 추출해 새로운 배열로 반환- 반환값을 명시적으로 설정interface Product { id: number; name: string; price: number; seller: { id: number; name: string; company: string; };}function getSellersFromProducts(products: any): any { return products.map((product) => product.seller);}function getSellersFromProducts(products: Product[]..
[TS] 인덱스드 액세스 타입
TIL
인덱스드 액세스 타입(Indexed Access Type)은 객체, 배열, 튜플 타입으로부터 특정 프로퍼티나 요소의 타입만 추출하는 타입이다.객체 프로퍼티 타입 추출게시글 작성자 정보를 출력하는 함수를 작성해보자.interface Post { title: string; content: string; author: { id: number; name: string; };}function printAuthorInfo(author: { id: number; name: string }) { console.log(`${author.name}-${author.id}`);} 만약 Post의 author 타입이 수정되면 함수의 매개변수 타입도 함께 수정해야 한다.인덱스드 액세스 타입을 사용하면 이 문..