basarat / typescript-book

:books: The definitive guide to TypeScript and possibly the best TypeScript book :book:. Free and Open Source 🌹
https://basarat.gitbook.io/typescript/
Other
20.77k stars 2.54k forks source link

making example of enum approach more standalone #630

Closed mungojam closed 2 years ago

mungojam commented 3 years ago

The use of string in both the enum and the structure made it a bit ambiguous whether the two types needed to match or not. The instructions above make it clear, but I figure it's nice to have an example that is unambiguous by itself.

The bit I'm not 100% on is the very last comment in this section, because I found that the IDE would show 'never' as the intersection between string enum and number but that the behaviour was still ok. So I didn't know if the comment was still valid for other structural types.

The workaround worked great for me anyway though, so thanks for posting this great guide!

mungojam commented 3 years ago

actually, check out this playground which shows that it interprets result as never (hover over result).

That's a bit of a shame as then consumers won't see the real type that they can then pass on to other functions that take it. I guess there isn't a workaround for that?

mungojam commented 2 years ago

Closing to clear my PR list