【问题标题】:Add new property inside interface object without changing class in typescript dynamically在接口对象内添加新属性而不动态更改打字稿中的类
【发布时间】:2020-05-03 06:36:58
【问题描述】:

我有一门课如下。

export class Cars implements Vehicales {
    color?: string;
    type?: string[];
}

由于某种原因,我无法修改上述导出对象。 模板对象被传递给 kendoReactGrid ,其中包含列的所有字符串,我只需要在我的界面中添加它,而不是导出对象。

我有一个界面如下

interface CarpProps {
    templates: Cars[]
}

现在我想向我的模板对象添加一个附加属性tyreCount: string[]。 我怎样才能在打字稿界面中做到这一点? 我在 extends 关键字期间尝试过,但没有成功。 我是打字稿的新手,任何帮助将不胜感激。

【问题讨论】:

    标签: reactjs typescript react-hooks


    【解决方案1】:

    您可以使用 extends 关键字代替 implements 并通过将 Cards 创建为接口来添加附加属性

    export interface Cars extends Vehicales {
        color?: string;
        type?: string[];
        tyreCount: string[]
    }
    

    并像使用它

    interface CarpProps {
        templates: Cars[]
    }
    

    【讨论】:

    • 感谢您的回复,但由于某种原因,我无法修改我的导出对象。模板对象被传递给 kendoReactGrid ,它包含列的所有字符串,我需要将它添加到我的模板中 - 仅接口而不是导出对象。有什么解决方法可以解决这个问题....向界面添加新属性?
    猜你喜欢
    • 2023-02-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2019-02-10
    • 2018-07-14
    • 2017-12-06
    • 2018-06-15
    • 2019-02-10
    相关资源
    最近更新 更多