在下面的代码中,智能感知工作正常
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'
这里也是图片形式:
知道为什么会发生这种情况,我该如何解决?
最佳答案
你在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/