Hacker Newsnew | past | comments | ask | show | jobs | submit | ryanoptimus's commentslogin

Looks like bongard problems for the referenced problem solving tasks https://en.wikipedia.org/wiki/Bongard_problem


The argument is the fact it can have additional properties at runtime. In which case does it not make more sense to have Object.keys typed as `(keyof T & string)[]` Where it's typed as the keys but allows for additional string values too?


This won't work - the code you've written here will forbid non string keys (i.e. symbols) but it won't allow arbitrary strings. If you changed it to a union rather than an intersection then that won't work either because the union of `"a" | "b" | string` is `string`. TS team made the correct call here, as annoying as it is.


The difference is IDE help: Even though the type itself ends up being "string", the IDE is able to offer you the individual values that you specify additionally if you ask for possible completions at places where that type is valid.

That's why I occasionally list specific options in addition to the larger parent type, just for the IDE, not for the type checks.

TypeScript mixes a lot of things and tools that are actually separate things and presents it in one big package, for better or worse, not just in this case.


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.


> 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).


Can 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.


Here 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

It also works when the type is defined in another file/module.

Playground: 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?


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: