Search code examples
javascriptvisual-studio-codeintellisensetypescript-typingsjsdoc

Getting intellisense error in VSCode, when setting a method of an Interface


In the following code, intellisense works fine

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;

But if I uncomment the //this.foo = this.foo; line I get the following warning on the returned object of the test function:

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'

Here it is in image form too:

Image Form

Any idea why this is happening how I can fix it?


Solution

  • You have declared bar in test.d.js, which is typeof ITest, but in your bar function you Don't have it.

    Try something like this and check:

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