【问题标题】:Extending Interface in TypeScript where module=es6 results in Property does not exist在 TypeScript 中扩展接口,其中 module=es6 导致属性不存在
【发布时间】: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


    【解决方案1】:

    在这里回答:https://github.com/Microsoft/TypeScript/issues/28687#issuecomment-442167850

    我需要将"moduleResolution" : "node" 添加到我的 tsconfig.json

    【讨论】:

      猜你喜欢
      • 2016-07-06
      • 2021-09-04
      • 2021-07-11
      • 2019-05-03
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      • 2018-10-13
      • 1970-01-01
      相关资源
      最近更新 更多