VS Code 打字稿:自动实现接口 [英] VS Code typescript: auto-implement interface

查看:99
本文介绍了VS Code 打字稿:自动实现接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在打字稿中有一个接口,想自动实现它.

我一直在环顾四周,根据

解决方案

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

interface IFoo {x?: 数字y?: 数字}类 Foo 实现了 IFoo {//没有显示代码操作}

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

interface IFoo {x:数字y?: 数字}类 Foo 实现了 IFoo {//代码动作会出现.//将同时实现 x 和 y?单击时}

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

I have an interface in typescript and want to autoimplement it.

I've been looking around and according to this stackoverflow question and github issue, this feature should already be there but it's not working for me. No lightbulb appears.

解决方案

If the interface has no required members, then the code action/quick fix will not appear on the class definition.

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

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

But if the interface has at least one required member, then the code action will appear, and when clicked, will implement all the members, including the nullable ones.

interface IFoo {
    x: number
    y?: number
}

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

This behavior is due to TypeScript's compiler, not VS Code. You can track this issue as it related to VS Code here, and the TypeScript functionality here.

这篇关于VS Code 打字稿:自动实现接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆