type-fest icon indicating copy to clipboard operation
type-fest copied to clipboard

`IsEqual` identity issues

Open trevorade opened this issue 1 year ago • 2 comments

There are two self-identity bugs that can pretty simply be fixed: TS Playground

Both of these evaluate to false:

IsEqual<{a: 1} & {a: 1}, {a: 1}>
IsEqual<{a: 1} | {a: 1}, {a: 1}>

The fix is to expand the check a touch:

type IsEqual<A, B> =
    (<G>() => G extends A & G | G ? 1 : 2) extends
    (<G>() => G extends B & G | G ? 1 : 2)
    ? true
    : false;

There's also this intersection bug but I'm unaware of a fix: TS Playground

This evaluates to false:

IsEqual<{a: 1} & {b: 2}, {a: 1, b: 2}>

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

trevorade avatar Jan 06 '23 03:01 trevorade

https://github.com/Microsoft/TypeScript/issues/27024#issuecomment-845655557 has a robust solution that seems to solve this (TS Playground link). It's a much longer type though, and seemingly not perfect either.

tommy-mitchell avatar Mar 02 '23 05:03 tommy-mitchell

Seemingly, according to Update 1 on this StackOverflow answer, under TypeScript's implementation {a: 1} & {b: 2} and {a: 1, b: 2} aren't actually the same. Not sure if that means they should or shouldn't be equal on the type level, though.

microsoft/TypeScript#48100 is an open issue tracking type equality and its definition. Seems that a recursive Simplify can fix some issues with equality as well.

tommy-mitchell avatar Mar 03 '23 04:03 tommy-mitchell