4 ms·
No, in this case the IDE won't help you. To make that happen you'd have to write this as: (keyof typeof someObject | (string & Record<never, never>))[] wh
by phpnode 3y ago
No, in this case the IDE won't help you. To make that happen you'd have to write this as:
(keyof typeof someObject | (string & Record<never, never>))[]
which will give you autocomplete but still allow arbitrary strings. TS will still complain as soon as you use it as an object indexer though, defeating the point.
- nosianu 3y ago> No, in this case the IDE won't help you. Yes it does! I even tried it just to be sure with just that example "a" | "b" | string. The Playground and Webstorm both provided correct completions, Playground added a lot of wrong useless stuff, Webstorm - using tsc service I guess - only "a" and "b" as possible values (there is nothing concrete to other string after all).
- phpnode 3y agoCan you share a playground link? When I try I do get the suggestion, but that suggestion is coming from the list of words in the file, not from typescript's language service. In the playground you can see the difference because the the autocomplete entry for the suggestion has an abc icon rather than a cube icon.
- nosianu 3y agoHere is a screenshot from my Webstorm IDE. The Playgrond is less useful, even when I had a demo it showed a thousand completely useless completions in addition to the two string constants and I'm not editing in there but in the IDE anyway so that is what matters. https://imgur.com/a/fntl6GX https://imgur.com/a/fntl6GX It also works when the type is defined in another file/module. Playground: https://www.typescriptlang.org/play?noUnusedLocals=true&noUnusedParameters=true&preserveConstEnums=true&target=99&jsx=0&pretty=true&q=283#code/KYDwDg9gTgLgBDAnmYcCyiBqBDANgV2ABVlUBeOAcm0rgB8qAjZ2hgZxigEsA7AcwDcAWABQogMYQeHOEQCiAZSIAudFjyESKOBUqVhYkZOkRcwAHS4IfABTylASgNA https://www.typescriptlang.org/play?noUnusedLocals=true&noUn... I don't know where exactly the suggestions come from, if it's the language service or something else. Why would that matter? The only point I care about is that those suggestions are made, making those useless-for-type-checks union components still useful. I found WebStorm to show them even after I unchecked the TS language service in the settings. So what?