这是我喜欢的类型:
export type Supply = {
id: number;
name: string;
manufacturer?: string;
model?: string;
}下面是我尝试为具有该类型的对象分配的方法:
return response['data']['supplies'].map((supply: ServerResponse.Supply) => {
let s = {
id: supply['supply_id'],
name: supply['name'],
}
if ('manufacturer' in supply) {
s.manufacturer = supply['manufacturer']
}
if ('model' in supply) {
s.model = supply['model']
}
return s;
});我收到TypeScript警告:
在类型'{ id: number;name: string;}‘上不存在ts属性“制造商”。 类型'{ id: number;name: string;}‘不存在ts属性’模型‘
我的密码怎么了?
发布于 2019-02-04 15:40:58
只需添加类型信息:
let s: Supply = {
id: supply['supply_id'],
name: supply['name'],
}否则TS将根据初始声明假设变量s有自己的类型{id: number, name: string}。
https://stackoverflow.com/questions/54519332
复制相似问题