VS Code typescript: auto-implement interface

前端 未结 1 1978
谎友^
谎友^ 2021-01-19 07:44

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

相关标签:
1条回答
  • 2021-01-19 08:10

    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.

    0 讨论(0)
提交回复
热议问题