Code:
import _ from 'lodash';
const BillTermTextMap = {
0: "0m",
1: "1m",
} as const;
type BillTerm = typeof BillTermTextMap;
type BillTermValues = BillTerm[keyof BillTerm];
type BillTermKey = keyof BillTerm;
type MarketReportDTO = {
amount: number;
billTerm: BillTermKey;
}
type TableRecord = { title: string } & Record<BillTermValues, MarketReportDTO>;
const marketReportDTOs: MarketReportDTO[] = [{ amount: 1, billTerm: 0 }, { amount: 2, billTerm: 1 }]
const tableRecord: TableRecord = Object.assign({ title: 'test' }, _.keyBy(marketReportDTOs, o => BillTermTextMap[o.billTerm]))
console.log('tableRecord: ', tableRecord)
// { title: 'test', '0m': { amount: 1, billTerm: 0 }, '1m': { amount: 2, billTerm: 1 } }
Got error:
Type '{ title: string; }' is not assignable to type 'TableRecord'.
Type '{ title: string; }' is missing the following properties from type 'Record<BillTermValues, MarketReportDTO>': "0m", "1m", "2m", "3m", and 3 more.(2322)
I am not sure it’s because the spread operator or the return type of the _.keyBy()
function.
TS Playground