我有:
interface Data {
[key: string]: number
}
interface MetaData {
date: Date
}
export type Datapoint = MetaData & Data
目前为止一切都很好。问题出现在我需要制作其中之一时:
const d: Datapoint = {date: new Date()}
-> error TS2322: Type '{ date: Date; }' is not assignable to type 'Datapoint'.
Type '{ date: Date; }' is not assignable to type 'Data'.
Property 'dd' is incompatible with index signature.
Type 'Date' is not assignable to type 'number'.
我该怎样解决这个问题?
{ [key: string]: number}
意味着每个属性都必须具有number
类型,没有任何规定可以像“但如果属性名称是date
,它必须是Date
而不是number
”这样。 - artem