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] 와 같은 방식으로 사용한 개념이라고 보면 된다.