简体   繁体   中英

TypeScript .d.ts definition for function with a callback parameter doesn't provide auto-completion in Intellij IDEA

I've been trying to set up some .d.ts definitions for a JavaScript project to add auto-completion to Intellij IDEA.

An example of the JavaScript code I'm trying to define is:

var testObj = {
    tests: function (it) {
        it("Should...", function (screen) {

        })
    }
};

where the 'screen' callback parameter should have auto-completion.

I've been successful in doing this by adding some JSDoc comments that link to the .d.ts interfaces:

var testObj = {
    /** @param {function(*,function(TestNamespace.IScreen))} it */
    tests: function (it) {
        it("Should...", function (screen) {

        })
    }
};

and

var testObj = {
    tests: function (it) {
        it("Should...",
            /** @param {TestNamespace.IIt} it */
            function (screen) {

            })
    }
};

The problem with the first solution being that it looks overly complicated and I'm sure it can be simplified with something like /** @param {TestNamespace.IIt} it */ but when linking to this interface the 'screen' object loses all relevant auto-completion..

The problem with the second solution is that I would have to repeat the comment on every 'it' block that gets created.

So what I'm wanting is to be able to use the first solution but without the long, complicated JSDoc comment but instead using a shorter JSDoc comment that links straight to the TestNamespace.IIt interface. I'm thinking that I've made a mistake with the IIt interface definition but I can't seem to figure out what I've done wrong. Hopefully what I want is possible. Any help would be greatly appreciated!

A simplified version of the current .d.ts file I have is:

export = TestNamespace;
declare namespace TestNamespace {
    export interface IIt {
        (description: string, callback: (screen: IScreen) => void): void
    }

    export interface IScreen {
        doSomething(): void
    }
}

You can specify the interface as type parameter and not use jsdoc at all:

var testObj = {
    tests: function(it) {
        it('Should...', function(screen: TestNamespace.IScreen) {});
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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