Adrien
Adrien

Reputation: 81

Why isn't {a: never} equivalent to never?

It seems like {a: never} should be able to be simplified to never. Doing so would fix my issues below. Is there a reason this isn't possible?

type A = {tag: 'A', value: number}
type B = {tag: 'B', value: boolean}
type N = {tag: never, value: string}

type Ntag = N["tag"] // never
type Nvalue = N["value"] // string
// Couldn't N["value"] be simplified to never because an object of type N could never exist?
// And thus, couldn't N be simplified to never?

type AN = A | N
type ANtag = AN["tag"] // "A"
type ANvalue = AN["value"] // Expected: number, Actual: string | number
// AN has to be an A, an object of type N can't exist. So why can't AN["value"] be simplified to number?

type AB = A | B
type NarrowToA = AB & {tag: 'A'} // Expected A, Actual: (A & {tag: "A"}) | (B & {tag: "A"})
type NarrowToATag = NarrowToA["tag"] // A
type NarrowToAValue = NarrowToA["value"] // Expected number, Actual number | boolean
// Again, NarrowToA has to be an A, an object of type B would never fit. So why can't NarrowToA["value"] be simplified to number?

Playground Link

Upvotes: 3

Views: 76

Answers (1)

Adrien
Adrien

Reputation: 81

Thank you for your comment @zerkms! It made me realize that the following is a valid value of type N. So it is possible for an object to exist that has a property of type never.

type N = {tag: never, value: string}

const n: N = {
    get tag(): never {
        while (true) {}
    },

    value: 'hi'
}

const nTag = n.tag; // Type: never
const nValue = n.value; // Type: string

Upvotes: 2

Related Questions