Martin Martin - 3 months ago 17
TypeScript Question

Typescript: No intellisense for a shared type

I am using a framework and I am looking at the property (called item) but it gives me no intellisense. Everything else works. After investigating this property to understand why it wasn't giving me feedback I notice that its set like so

item: TG.BaseItem | TG.ClosedItem | any;


So I think the problem is that Typescript doesn't know what type it is. Initially it is NULL and I actually call another function that sets the type so I know what the type is.

I have also tested the js at run-time and its working the way it should.

Is there anyway to give typescript a helping hand so it can give me the intellisense for TB.ClosedItem, as I know thats what it is.

I want to try and avoid changing the item signature, it doesn' belong to me and the fact of the matter is that the item can be a number of things depending on certain things. Although I always know what it is so if i can tell typescript what it is this would be great.

my object is collectables so I do a

collectables.item.#No additional properties shown#


so when I do collectables and a dot I get a long list of properties and functions inside the collectables. Then I choose item and then a dot and only things that are shown are standard JS things like HasOwnProperty, Constructor but none of the properties/functions of the item.

Anyone any ideas on this ?

Thanks

Answer

code example with completion The picture above shows you that completion works on union types in case you help typescript to narrow down the type (further detailes here). As soon as you add the any type to the definition (| any to line 9), typescript will not be able to give you code completion. Union was created to help replace those annoying any's and narrow down the options. In your case just defining the type to be any would be equivalent to BaseItem | ClosedItem | any.

Comments