首页 > 解决方案 > 如何让 VSCode/Typescript 对导入的类进行自动完成?

问题描述

假设我有一个文件,class.ts

//I tried implementing interface, but it won't help, with or without the interface
interface FooInterface {
    bar: Function
}

class Foo implements FooInterface {

    bar() {
        console.log('Hello world!');
    }

}

export = new Foo();

从我的另一个文件中index.js,我希望 VS Code 能够读取类中的内容Foo并自动完成,但事实并非如此。

const Foo = require('./class');

///Expecting the .bar() suggestion after typing Foo., but it didn't happen
Foo.bar();

我错过了什么吗?我应该怎么做才能让 VS Code 识别类中可用的方法/属性?许多第三方库都能够做到这一点。

标签: typescriptclassvisual-studio-codeimportexport

解决方案


这个模式对我来说有点奇怪,所以我为你创建了一个沙箱,

希望它会有所帮助

这是沙箱:https ://codesandbox.io/s/vigorous-fog-hngl5?file=/src/index.js:0-72

如果您想查看差异,我还将在此处粘贴代码

interface FooInterface {
  bar: () => void
}

export default class Foo implements FooInterface {

  bar() {
      console.log('Hello world!');
  }

}

这是您导入它的方式:

import Foo from './class';

const FooInstance = new Foo();

这是结果: 在此处输入图像描述


推荐阅读