Logo

dev-resources.site

for different kinds of informations.

Why does typescript not conditionally pick the right type in a union?

Published at
1/10/2020
Categories
typescript
help
question
Author
Thibault Maekelbergh
Categories
3 categories in total
typescript
open
help
open
question
open
Why does typescript not conditionally pick the right type in a union?

Why does the | (Union) operator not work as a logical or?

In the following snippets I would've expected it to see that the name property is available in 1 of the provided union options? Instead it errors that it can't find name because it is not available in MyDevice but it is available in the interface provided in the union.

How do you go about conditionally deciding to pick the interface instead of MyDevice when the name property is available?

I know conditional types exist but that doesn't seem like an option because I'm providing plain interfaces and not types/interfaces with generics.

img

Alt Text

Featured ones: