【问题标题】:VS Code typescript: auto-implement interfaceVS Code typescript:自动实现界面
【发布时间】:2017-12-02 19:57:33
【问题描述】:

我在 typescript 中有一个接口,想自动实现它。

我一直在环顾四周,根据this stackoverflow questiongithub issue, 的说法,这个功能应该已经存在,但它对我不起作用。没有灯泡出现。

【问题讨论】:

  • 您写入的文件是否带有 .ts 扩展名?因为我有 VsCode 1.13.1 版本的灯泡

标签: typescript visual-studio-code


【解决方案1】:

如果接口没有必需的成员,则代码操作/快速修复将不会出现在类定义中。

interface IFoo {
    x?: number
    y?: number
}

class Foo implements IFoo {
    // no code action shown
}

但是如果接口至少有一个必需的成员,那么代码动作就会出现,当点击时,将实现所有成员,包括可以为空的成员。

interface IFoo {
    x: number
    y?: number
}

class Foo implements IFoo {
    // code action will appear.
    // will implement both x and y? when clicked
}

这种行为是由于 TypeScript 的编译器,而不是 VS Code。您可以跟踪此问题,因为它与 VS Code here 和 TypeScript 功能 here 有关。

【讨论】:

    猜你喜欢
    • 2021-06-29
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 2013-08-20
    相关资源
    最近更新 更多