#Why does this type equality testing test return boolean instead of true?

12 messages · Page 1 of 1 (latest)

granite mortarBOT
#
sandiford#0

Preview:```ts
type Equal<A, B> = A extends B
? B extends A
? true
: false
: false
type T = Equal<string | boolean, string | boolean>
// ^?

type ObjectsEqual<T, U> = keyof T extends keyof U
? keyof U extends keyof T
? true
: false
: false
type U = ObjectsEqual<{a: 1; b: 2}, {a: 1; b: 1}>
...```

ashen quarry
#

Both seem to be testing equality of unions using extends both ways. But the first gives boolean result, while the latter gives true

#

I'm confused

ancient inlet
#

you're encountering distribution

#

!hb dist

granite mortarBOT
ancient inlet
#

generally equality checks will use [A, B] extends [B, A] for this as far as ive seen

#

then the other one could be just using Equal<keyof A, keyof B>

ashen quarry
#

Oh that's annoying

#
type Equal<A, B> = [A] extends [B]
    ? ([B] extends [A]
        ? true
        : false
    )
    : false
type T = Equal<string | boolean, string | boolean>
  // ^?
#

this works fine

#

I wish TS was more explicit