clencat
clencat

Reputation: 159

why typescript string & number show never?

I was studing Typescript, but I have some confusion.

type t = number & string // never
type t1 = number & boolean // never
type t2 = number & string[] // number & string[]

Why does it look different?

Upvotes: 6

Views: 99

Answers (1)

Goutham J.M
Goutham J.M

Reputation: 2194

In Typescipt the instersection & of two primitive types is always never because a variabe cannot be both string and a number, but the intersection of two array/object is called as branded object and one array/object with a primitive is a valid type and is called as branded primitve , so where do we use branded primitive ? , refer the below example

type SomeUrl = string & {'this is a url': {}};

let x = <SomeUrl>'';

Upvotes: 2

Related Questions