Bidisha Das
Bidisha Das

Reputation: 402

Why does this return 'never' type?

I found something really interesting.

type q = number & string;

As you can see, on hovering over q, I see it is of type 'never'. Why is this so?

Upvotes: 2

Views: 755

Answers (2)

mecode4food
mecode4food

Reputation: 320

The idea is that never means that the variable will never happen/occur, so when you declare a variable to be an intersection (&) of both number and string, TypeScript will try to find types that are both number and string at the same time...

Which will never happen, as the two types do not intersect. I guess an equivalent analog is that you are trying to find something that is both a left hand and a right hand, which will never happen.

Upvotes: 3

Siddharth
Siddharth

Reputation: 469

A value can never be of type number and string. Hence, its type is never.

Upvotes: 0

Related Questions