Skip to content

Commit b8fbf88

Browse files
authored
Merge pull request microsoft#19926 from Microsoft/improvePrimitiveComparable
Bidirectional comparable relation for primitive types
2 parents d2eb8ba + 16b68ff commit b8fbf88

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

src/compiler/checker.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -8907,7 +8907,9 @@ namespace ts {
89078907
if (target.flags & TypeFlags.StringOrNumberLiteral && target.flags & TypeFlags.FreshLiteral) {
89088908
target = (<LiteralType>target).regularType;
89098909
}
8910-
if (source === target || relation !== identityRelation && isSimpleTypeRelatedTo(source, target, relation)) {
8910+
if (source === target ||
8911+
relation === comparableRelation && !(target.flags & TypeFlags.Never) && isSimpleTypeRelatedTo(target, source, relation) ||
8912+
relation !== identityRelation && isSimpleTypeRelatedTo(source, target, relation)) {
89118913
return true;
89128914
}
89138915
if (source.flags & TypeFlags.Object && target.flags & TypeFlags.Object) {
@@ -9050,7 +9052,8 @@ namespace ts {
90509052
return isIdenticalTo(source, target);
90519053
}
90529054

9053-
if (isSimpleTypeRelatedTo(source, target, relation, reportErrors ? reportError : undefined)) return Ternary.True;
9055+
if (relation === comparableRelation && !(target.flags & TypeFlags.Never) && isSimpleTypeRelatedTo(target, source, relation) ||
9056+
isSimpleTypeRelatedTo(source, target, relation, reportErrors ? reportError : undefined)) return Ternary.True;
90549057

90559058
if (isObjectLiteralType(source) && source.flags & TypeFlags.FreshLiteral) {
90569059
if (hasExcessProperties(<FreshObjectLiteralType>source, target, reportErrors)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [independentPropertyVariance.ts]
2+
// Verify that properties can vary independently in comparable relationship
3+
4+
declare const x: { a: 1, b: string };
5+
declare const y: { a: number, b: 'a' };
6+
7+
x === y;
8+
9+
10+
//// [independentPropertyVariance.js]
11+
"use strict";
12+
// Verify that properties can vary independently in comparable relationship
13+
x === y;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/conformance/types/typeRelationships/comparable/independentPropertyVariance.ts ===
2+
// Verify that properties can vary independently in comparable relationship
3+
4+
declare const x: { a: 1, b: string };
5+
>x : Symbol(x, Decl(independentPropertyVariance.ts, 2, 13))
6+
>a : Symbol(a, Decl(independentPropertyVariance.ts, 2, 18))
7+
>b : Symbol(b, Decl(independentPropertyVariance.ts, 2, 24))
8+
9+
declare const y: { a: number, b: 'a' };
10+
>y : Symbol(y, Decl(independentPropertyVariance.ts, 3, 13))
11+
>a : Symbol(a, Decl(independentPropertyVariance.ts, 3, 18))
12+
>b : Symbol(b, Decl(independentPropertyVariance.ts, 3, 29))
13+
14+
x === y;
15+
>x : Symbol(x, Decl(independentPropertyVariance.ts, 2, 13))
16+
>y : Symbol(y, Decl(independentPropertyVariance.ts, 3, 13))
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/conformance/types/typeRelationships/comparable/independentPropertyVariance.ts ===
2+
// Verify that properties can vary independently in comparable relationship
3+
4+
declare const x: { a: 1, b: string };
5+
>x : { a: 1; b: string; }
6+
>a : 1
7+
>b : string
8+
9+
declare const y: { a: number, b: 'a' };
10+
>y : { a: number; b: "a"; }
11+
>a : number
12+
>b : "a"
13+
14+
x === y;
15+
>x === y : boolean
16+
>x : { a: 1; b: string; }
17+
>y : { a: number; b: "a"; }
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @strict: true
2+
3+
// Verify that properties can vary independently in comparable relationship
4+
5+
declare const x: { a: 1, b: string };
6+
declare const y: { a: number, b: 'a' };
7+
8+
x === y;

0 commit comments

Comments
 (0)