【发布时间】:2018-07-13 07:03:05
【问题描述】:
我刚刚开始使用 Typescript,并且遇到了一个奇怪的问题。
我已经声明了一个这样的接口:
Location.d.ts
interface Location {
lat: number;
lng: number;
}
然后这样使用这个接口:
reducer.tsx
interface MapState extends State {
locations: Location[];
}
现在,我尝试声明一个类型为 MapState 的变量,但出现错误:
Type ... 不可分配给类型“MapState”。属性“位置”的类型不兼容。类型 ... 不可分配给类型“位置 []”。类型 ... 不可分配给类型“位置”。类型中缺少属性“哈希”...
这是我的声明:
const initialState: MapState = {
locations: [
{ lat: 33.488069, lng: -112.072972 }
]
};
如果我明确地转换 Location,那么它可以工作:
const initialState: MapState = {
locations: [
({ lat: 33.488069, lng: -112.072972 } as Location)
]
};
但这非常冗长。我在这里遗漏了什么吗?
【问题讨论】:
标签: typescript interface