【发布时间】:2019-04-29 05:26:36
【问题描述】:
当我尝试在 tsconfig.json 中从 "module": "commonjs", 切换到 "module: "es6", 时,由于以下错误,我无法再编译以下代码:
models/Combined.ts(12,9): error TS2322: Type '{ 'name': any; '日期': 日期; “职业”:任何; }' 不可分配给类型“组合”。
对象字面量只能指定已知属性,而 ''name'' 可以 在“组合”类型中不存在。models/Combined.ts(22,23):错误 TS2339:“组合”类型上不存在属性“名称”。
模型/Combined.ts:
import {
Simple
} from './';
export interface Combined extends Simple {
occupation?: string;
}
export function CombinedFromJSON(json: any): Combined {
return {
'name': json['Name'],
'date': !exists(json, 'Date') ? undefined : new Date(json['Date']),
'occupation': !exists(json, 'Occupation') ? undefined : json['Occupation'],
};
}
export function CombinedToJSON(value?: Combined): any {
if (value === undefined) {
return undefined;
}
return {
'Name': value.name,
'Occupation': value.occupation,
};
}
模型/Simple.ts
export interface Simple {
name: string;
readonly date?: Date;
}
我已经阅读了一些与此类似的 SO 问题以及来自 typescript 网站的interfaces page,但我仍然对这个问题摸不着头脑。看起来这应该可以工作,而且当目标模块系统是commonjs 时确实可以。 tslint 在开发时完全没有抱怨,我尝试了 TS 2.4 和 3.1 都没有运气。
任何帮助/指导将不胜感激!
【问题讨论】:
标签: typescript