简体   繁体   English

VSCode中的ES6代码完成(Intellisense)

[英]ES6 code completion (Intellisense) with composition in VSCode

I am trying to get code completion to work when nesting classes in es6 using the following syntax: class Dog extends FoodMixin(Animal) . 我正在尝试使用以下语法在es6中嵌套类时使代码完成工作: class Dog extends FoodMixin(Animal) The first implementation works fine, giving me auto completion for both FoodMixin and Animal. 第一个实现工作正常,可以自动完成FoodMixin和Animal。 However, if I nest it one deeper, or apply a second mixin, the auto completion stops. 但是,如果我将其嵌套更深,或者应用第二个mixin,则自动完成功能会停止。 For instance: class Dog extends OtherMixin(FoodMixin(Animal)) will lose the code completion for the FoodMixin class. 例如: class Dog extends OtherMixin(FoodMixin(Animal))将失去FoodMixin类的代码完成。

Is there a way I can have intellisense work for both OtherMixin and FoodMixin? 有什么办法可以让OtherMixin和FoodMixin拥有智能感知功能?

Simple test code: 简单的测试代码:

const FoodMixin = superclass => class extends superclass {
    eat() {
        console.log("Eating");
    }
};

const OtherMixin = superclass => class extends superclass {
    test() {
        console.log("Hello");
    }
};

class Animal {
}

class Dog extends OtherMixin(FoodMixin(Animal)){
}

const dog = new Dog();
dog.test(); //INTELLISENSE WORKS
dog.eat(); //INTELLISENSE DOES NOT WORK

VS Code's JavaScript IntelliSense will not be able to understand very dynamic code like that example. VS Code的JavaScript IntelliSense将无法理解像该示例这样的非常动态的代码。 The tutorial you linked to that provided this code is being too clever for its own (or really anyone's) good. 您所链接的教程提供的这段代码太聪明了,以至于它本身(或任何人)都没有。

However you can sort of workaround VS Code's limitation by adding explicit type annotations for the types and interfaces used in the example using JSDoc: 但是,您可以通过使用JSDoc为示例中使用的类型和接口添加显式类型注释来解决VS Code的局限性:

/**
 * @typedef {{ eat(): void }} Eater
 */

/**
 * @typedef {{ test(): void }} Tester
 */

/**
 * @type {Dog & Eater & Tester}
 */
const dog = new Dog();
dog.

在此处输入图片说明

The & is not standard JS Doc type syntax but an intersection type from TypeScript. &不是标准的JS Doc类型语法,而是TypeScript中的交集类型 (I am using it here because it mimics composition) (我在这里使用它是因为它模仿成分)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM