javascript - 设置接口(interface)方法时,在 VSCode 中出现智能感知错误

标签 javascript visual-studio-code intellisense typescript-typings jsdoc

在下面的代码中,智能感知工作正常

test.d.ts:

export interface ITest {
    foo: string;
    setFoo(foo: string): ITest;
}

export as namespace JSDoc;

test.js:

/** @typeof {import("./test")} JSDoc */

/**
 * @returns {JSDoc.ITest}
 */
function test() {
    return {
        foo: "",
        setFoo: function (foo) {
            this.foo = foo;
            return this;
        }
    };
}

exports.test = test;

但是如果我取消注释 //this.foo = this.foo; 行,我会在 test 函数的返回对象上收到以下警告:

Type 'typeof setFoo' is not assignable to type '(foo: string) => ITest'. Property 'setFoo' is missing in type 'setFoo' but required in type 'ITest'.ts(2322) test.d.ts(3, 5): 'setFoo' is declared here. test.d.ts(3, 5): The expected type comes from property 'setFoo' which is declared here on type 'ITest'

这里也是图片形式:

Image Form

知道为什么会发生这种情况,我该如何解决?

最佳答案

你在test.d.js中声明了bar,它是typeof ITest,但是在你的bar 你没有的功能。

尝试这样的事情并检查:

function bar() {
    this.foo = this.foo;
    this.bar = this;
    return this;
}

关于javascript - 设置接口(interface)方法时,在 VSCode 中出现智能感知错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57324984/

相关文章:

node.js - VS Code 未验证断点

mysql - 是否可以在 Visual Studio 2010 中停止 .sql 文件的智能感知?

javascript - 推特位置未定义

git - 如何在VSCode中更改用户并链接我的Github帐户?

javascript - 通过使用 Map Filter 与对象数组进行比较来返回字段

Ubuntu 18 VS Code 错误 Enoent : no such directory, lstat'/snap/code'

visual-studio-code - VS Code 智能感知的方式

javascript - Intellisense : view file as browser JavaScript, 不是 Node.js?

javascript - json 键中的动态名称

javascript - 网速慢时如何显示基本的html页面?