HooneyLog
© 2026 Seunghoon Shin. All rights reserved.
모든 게시글
typescript
2022. 5. 20.•
3

Interface에서 어떠한 key의 value를 타입으로 만드는 방법

Seunghoon Shin
작성자 Seunghoon Shin풀스택 개발자

어떠한 Interface인가?

interface Data { report: { id: string; source: string; amount: number; created_at: Date; updated_at: Date; type: DataType; }[]; }

위와 같은 Data라는 Interface가 있다고 가정해볼때, report의 객체 배열이 아닌 객체에 대한 타입을 Interface를 사용하여 어떻게 타입을 추출할 수 있을까 고민을 해봤다.

해결 방안

const newReport: Data['report'][number] = { id: uuid(), created_at: new Date(), updated_at: new Date(), type: 'income', amount: 400, source: 'HooneyLog', };

몇가지의 시도를 해보다가 Data['report'][number] 와 같은 형태로 타입을 만들어보니까 잘 작동한다는것을 확인 할 수 있었다.

여기서 [number]를 넣은 이유는 위 Data라는 interface에서 report의 타입이 배열 형태이기 때문에 배열 형태에서 해제를 시키기 위해 사용한것이다.

흔히 배열에서 어떠한 값을 슬라이싱을 할때 array[0] 와 같은 방식으로 사용한 개념이라고 보면 된다.

← 이전 글Union Type Guard 하는 방법
다음 글 →TypeScript 객체 업데이트의 정석: Spread 연산자와 Utility Types(Pick, Partial) 활용하기