WebStorm and TypeScript intellisense

2019-09-01 15:29发布

问题:

I am trying to get intellisense for TypeScript working in WebStorm.

In my test here I am trying to get it for Knockout.

I have added the type definition file and other files necessary, added the reference tag to the top of my .ts file.

Here is a screenshot of my setup:

When I type 'ko.' I can scroll through the list and find observable but other than that I get no other intellisense.

Is there something wrong with my setup? Am I wrong to expect overloads and such when I open a paren after typing 'ko.observable'?

If I change the line

declare var ko : KnockoutState

to

declare var ko : KnockoutObservable<string>;

I get the error

'Error:(3, 13) TS2403: Subsequent variable declarations must have the same type. Variable 'ko' must be of type 'KnockoutStatic', but here has type 'KnockoutObservable'.

(UPDATE) NOTE: I mention the above error only to show that it does seem to be reading the type definition for Knockout. I still do not get any intellisense when I leave it at KnockoutStatic or remove the type out completely.

回答1:

When I type 'ko.' I can scroll through the list and find observable but other than that I get no other intellisense.

You need to use it in code to get intellisense. I can see you tried that with :

declare var ko : KnockoutObservable<string>;

Which is an invalid usage ... and hence you got the error TS2403.

Fix

Use ko properly:

var myObservableArray = ko.observableArray<any>();  // intellisense on `ko.`
myObservableArray.push('Some value');               // intellisense on `.p`
var anotherObservableArray = ko.observableArray([
    { name: "Bungle", type: "Bear" },
    { name: "George", type: "Hippo" },
    { name: "Zippy", type: "Unknown" }
]);