-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make identity comparison reflexive across unions or intersections of …
…the same type
- Loading branch information
MichaelMitchell-at
committed
Dec 29, 2024
1 parent
56a0825
commit 7b7a9b2
Showing
5 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//// [tests/cases/compiler/reflexiveIdentityRelation.ts] //// | ||
|
||
//// [reflexiveIdentityRelation.ts] | ||
namespace reflexiveIdentityRelation { | ||
type Equals<A, B> = (<T>() => T extends B ? 1 : 0) extends (<T>() => T extends A ? 1 : 0) ? true : false; | ||
|
||
type Intersection = Equals<{a: 1} & {a: 1}, {a: 1}>; // true | ||
type Union = Equals<{a: 1} | {a: 1}, {a: 1}>; // true | ||
type UnionOfIntersection = Equals<{a: 1} & {b: 2} | {a: 1} & {b: 2}, {a: 1} & {b: 2}>; // true | ||
|
||
// The intersection distributes to `{a: 1} & {a: 1} | {a: 1} & {b: 2} | {b: 2} & {a: 1} | {b: 2} & {b: 2}` | ||
// which is not identical to `{a: 1} | {b: 2}` | ||
type IntersectionOfUnion = Equals<({a: 1} | {b: 2}) & ({a: 1} | {b: 2}), {a: 1} | {b: 2}>; // false | ||
} | ||
|
||
|
||
//// [reflexiveIdentityRelation.js] | ||
"use strict"; | ||
|
||
|
||
//// [reflexiveIdentityRelation.d.ts] | ||
declare namespace reflexiveIdentityRelation { | ||
} |
54 changes: 54 additions & 0 deletions
54
tests/baselines/reference/reflexiveIdentityRelation.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//// [tests/cases/compiler/reflexiveIdentityRelation.ts] //// | ||
|
||
=== reflexiveIdentityRelation.ts === | ||
namespace reflexiveIdentityRelation { | ||
>reflexiveIdentityRelation : Symbol(reflexiveIdentityRelation, Decl(reflexiveIdentityRelation.ts, 0, 0)) | ||
|
||
type Equals<A, B> = (<T>() => T extends B ? 1 : 0) extends (<T>() => T extends A ? 1 : 0) ? true : false; | ||
>Equals : Symbol(Equals, Decl(reflexiveIdentityRelation.ts, 0, 37)) | ||
>A : Symbol(A, Decl(reflexiveIdentityRelation.ts, 1, 16)) | ||
>B : Symbol(B, Decl(reflexiveIdentityRelation.ts, 1, 18)) | ||
>T : Symbol(T, Decl(reflexiveIdentityRelation.ts, 1, 26)) | ||
>T : Symbol(T, Decl(reflexiveIdentityRelation.ts, 1, 26)) | ||
>B : Symbol(B, Decl(reflexiveIdentityRelation.ts, 1, 18)) | ||
>T : Symbol(T, Decl(reflexiveIdentityRelation.ts, 1, 65)) | ||
>T : Symbol(T, Decl(reflexiveIdentityRelation.ts, 1, 65)) | ||
>A : Symbol(A, Decl(reflexiveIdentityRelation.ts, 1, 16)) | ||
|
||
type Intersection = Equals<{a: 1} & {a: 1}, {a: 1}>; // true | ||
>Intersection : Symbol(Intersection, Decl(reflexiveIdentityRelation.ts, 1, 109)) | ||
>Equals : Symbol(Equals, Decl(reflexiveIdentityRelation.ts, 0, 37)) | ||
>a : Symbol(a, Decl(reflexiveIdentityRelation.ts, 3, 32)) | ||
>a : Symbol(a, Decl(reflexiveIdentityRelation.ts, 3, 41)) | ||
>a : Symbol(a, Decl(reflexiveIdentityRelation.ts, 3, 49)) | ||
|
||
type Union = Equals<{a: 1} | {a: 1}, {a: 1}>; // true | ||
>Union : Symbol(Union, Decl(reflexiveIdentityRelation.ts, 3, 56)) | ||
>Equals : Symbol(Equals, Decl(reflexiveIdentityRelation.ts, 0, 37)) | ||
>a : Symbol(a, Decl(reflexiveIdentityRelation.ts, 4, 25)) | ||
>a : Symbol(a, Decl(reflexiveIdentityRelation.ts, 4, 34)) | ||
>a : Symbol(a, Decl(reflexiveIdentityRelation.ts, 4, 42)) | ||
|
||
type UnionOfIntersection = Equals<{a: 1} & {b: 2} | {a: 1} & {b: 2}, {a: 1} & {b: 2}>; // true | ||
>UnionOfIntersection : Symbol(UnionOfIntersection, Decl(reflexiveIdentityRelation.ts, 4, 49)) | ||
>Equals : Symbol(Equals, Decl(reflexiveIdentityRelation.ts, 0, 37)) | ||
>a : Symbol(a, Decl(reflexiveIdentityRelation.ts, 5, 39)) | ||
>b : Symbol(b, Decl(reflexiveIdentityRelation.ts, 5, 48)) | ||
>a : Symbol(a, Decl(reflexiveIdentityRelation.ts, 5, 57)) | ||
>b : Symbol(b, Decl(reflexiveIdentityRelation.ts, 5, 66)) | ||
>a : Symbol(a, Decl(reflexiveIdentityRelation.ts, 5, 74)) | ||
>b : Symbol(b, Decl(reflexiveIdentityRelation.ts, 5, 83)) | ||
|
||
// The intersection distributes to `{a: 1} & {a: 1} | {a: 1} & {b: 2} | {b: 2} & {a: 1} | {b: 2} & {b: 2}` | ||
// which is not identical to `{a: 1} | {b: 2}` | ||
type IntersectionOfUnion = Equals<({a: 1} | {b: 2}) & ({a: 1} | {b: 2}), {a: 1} | {b: 2}>; // false | ||
>IntersectionOfUnion : Symbol(IntersectionOfUnion, Decl(reflexiveIdentityRelation.ts, 5, 90)) | ||
>Equals : Symbol(Equals, Decl(reflexiveIdentityRelation.ts, 0, 37)) | ||
>a : Symbol(a, Decl(reflexiveIdentityRelation.ts, 9, 40)) | ||
>b : Symbol(b, Decl(reflexiveIdentityRelation.ts, 9, 49)) | ||
>a : Symbol(a, Decl(reflexiveIdentityRelation.ts, 9, 60)) | ||
>b : Symbol(b, Decl(reflexiveIdentityRelation.ts, 9, 69)) | ||
>a : Symbol(a, Decl(reflexiveIdentityRelation.ts, 9, 78)) | ||
>b : Symbol(b, Decl(reflexiveIdentityRelation.ts, 9, 87)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//// [tests/cases/compiler/reflexiveIdentityRelation.ts] //// | ||
|
||
=== reflexiveIdentityRelation.ts === | ||
namespace reflexiveIdentityRelation { | ||
type Equals<A, B> = (<T>() => T extends B ? 1 : 0) extends (<T>() => T extends A ? 1 : 0) ? true : false; | ||
>Equals : Equals<A, B> | ||
> : ^^^^^^^^^^^^ | ||
>true : true | ||
> : ^^^^ | ||
>false : false | ||
> : ^^^^^ | ||
|
||
type Intersection = Equals<{a: 1} & {a: 1}, {a: 1}>; // true | ||
>Intersection : true | ||
> : ^^^^ | ||
>a : 1 | ||
> : ^ | ||
>a : 1 | ||
> : ^ | ||
>a : 1 | ||
> : ^ | ||
|
||
type Union = Equals<{a: 1} | {a: 1}, {a: 1}>; // true | ||
>Union : true | ||
> : ^^^^ | ||
>a : 1 | ||
> : ^ | ||
>a : 1 | ||
> : ^ | ||
>a : 1 | ||
> : ^ | ||
|
||
type UnionOfIntersection = Equals<{a: 1} & {b: 2} | {a: 1} & {b: 2}, {a: 1} & {b: 2}>; // true | ||
>UnionOfIntersection : true | ||
> : ^^^^ | ||
>a : 1 | ||
> : ^ | ||
>b : 2 | ||
> : ^ | ||
>a : 1 | ||
> : ^ | ||
>b : 2 | ||
> : ^ | ||
>a : 1 | ||
> : ^ | ||
>b : 2 | ||
> : ^ | ||
|
||
// The intersection distributes to `{a: 1} & {a: 1} | {a: 1} & {b: 2} | {b: 2} & {a: 1} | {b: 2} & {b: 2}` | ||
// which is not identical to `{a: 1} | {b: 2}` | ||
type IntersectionOfUnion = Equals<({a: 1} | {b: 2}) & ({a: 1} | {b: 2}), {a: 1} | {b: 2}>; // false | ||
>IntersectionOfUnion : false | ||
> : ^^^^^ | ||
>a : 1 | ||
> : ^ | ||
>b : 2 | ||
> : ^ | ||
>a : 1 | ||
> : ^ | ||
>b : 2 | ||
> : ^ | ||
>a : 1 | ||
> : ^ | ||
>b : 2 | ||
> : ^ | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// @strict: true | ||
// @declaration: true | ||
|
||
namespace reflexiveIdentityRelation { | ||
type Equals<A, B> = (<T>() => T extends B ? 1 : 0) extends (<T>() => T extends A ? 1 : 0) ? true : false; | ||
|
||
type Intersection = Equals<{a: 1} & {a: 1}, {a: 1}>; // true | ||
type Union = Equals<{a: 1} | {a: 1}, {a: 1}>; // true | ||
type UnionOfIntersection = Equals<{a: 1} & {b: 2} | {a: 1} & {b: 2}, {a: 1} & {b: 2}>; // true | ||
|
||
// The intersection distributes to `{a: 1} & {a: 1} | {a: 1} & {b: 2} | {b: 2} & {a: 1} | {b: 2} & {b: 2}` | ||
// which is not identical to `{a: 1} | {b: 2}` | ||
type IntersectionOfUnion = Equals<({a: 1} | {b: 2}) & ({a: 1} | {b: 2}), {a: 1} | {b: 2}>; // false | ||
} |