Skip to content

Commit 026b115

Browse files
committed
refactor: replaced check func with isTypeAssignableTo
1 parent 4507d21 commit 026b115

File tree

31 files changed

+3659
-37
lines changed

31 files changed

+3659
-37
lines changed

src/compiler/checker.ts

+21-19
Original file line numberDiff line numberDiff line change
@@ -20049,24 +20049,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2004920049
return isTypeComparableTo(type1, type2) || isTypeComparableTo(type2, type1);
2005020050
}
2005120051

20052-
function checkTypeComparableWithLessOrGreaterThanOperator(valueType: Type): boolean {
20053-
const t = valueType.flags;
20054-
return !!(
20055-
(t & TypeFlags.Number ||
20056-
t & TypeFlags.NumberLiteral ||
20057-
t & TypeFlags.BigInt ||
20058-
t & TypeFlags.BigIntLiteral ||
20059-
t & TypeFlags.String ||
20060-
t & TypeFlags.StringLiteral ||
20061-
t & TypeFlags.Any ||
20062-
t & TypeFlags.TypeParameter ||
20063-
t & TypeFlags.Object ||
20064-
t & TypeFlags.Intersection ||
20065-
t & TypeFlags.Union) &&
20066-
!(t & TypeFlags.Boolean)
20067-
);
20068-
}
20069-
2007020052
function checkTypeAssignableTo(source: Type, target: Type, errorNode: Node | undefined, headMessage?: DiagnosticMessage, containingMessageChain?: () => DiagnosticMessageChain | undefined, errorOutputObject?: { errors?: Diagnostic[]; }): boolean {
2007120053
return checkTypeRelatedTo(source, target, assignableRelation, errorNode, headMessage, containingMessageChain, errorOutputObject);
2007220054
}
@@ -38331,7 +38313,27 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3833138313
leftType = getBaseTypeOfLiteralTypeForComparison(checkNonNullType(leftType, left));
3833238314
rightType = getBaseTypeOfLiteralTypeForComparison(checkNonNullType(rightType, right));
3833338315
reportOperatorErrorUnless((left, right) => {
38334-
if (!checkTypeComparableWithLessOrGreaterThanOperator(left) || !checkTypeComparableWithLessOrGreaterThanOperator(right)) {
38316+
const isLeftTypeComparable = !!(
38317+
isTypeAssignableTo(left, numberOrBigIntType) ||
38318+
isTypeAssignableTo(left, stringType) ||
38319+
isTypeAssignableTo(left, anyType) &&
38320+
!isTypeAssignableTo(left, voidType) &&
38321+
!isTypeAssignableTo(left, booleanType) &&
38322+
!isTypeAssignableTo(left, emptyGenericType) &&
38323+
!(left.flags & TypeFlags.Object)
38324+
);
38325+
38326+
const isRightTypeComparable = !!(
38327+
isTypeAssignableTo(right, numberOrBigIntType) ||
38328+
isTypeAssignableTo(right, stringType) ||
38329+
isTypeAssignableTo(right, anyType) &&
38330+
!isTypeAssignableTo(right, voidType) &&
38331+
!isTypeAssignableTo(right, booleanType) &&
38332+
!isTypeAssignableTo(right, emptyGenericType) &&
38333+
!(right.flags & TypeFlags.Object)
38334+
);
38335+
38336+
if (!isLeftTypeComparable || !isRightTypeComparable) {
3833538337
return false;
3833638338
}
3833738339

tests/baselines/reference/ambiguousGenericAssertion1.errors.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ ambiguousGenericAssertion1.ts(4,15): error TS2304: Cannot find name 'x'.
33
ambiguousGenericAssertion1.ts(4,16): error TS1005: ')' expected.
44
ambiguousGenericAssertion1.ts(4,19): error TS1005: ',' expected.
55
ambiguousGenericAssertion1.ts(4,21): error TS1005: ';' expected.
6+
ambiguousGenericAssertion1.ts(4,24): error TS2365: Operator '>' cannot be applied to types 'any' and '<T>(x: T) => T'.
67

78

8-
==== ambiguousGenericAssertion1.ts (5 errors) ====
9+
==== ambiguousGenericAssertion1.ts (6 errors) ====
910
function f<T>(x: T): T { return null; }
1011
var r = <T>(x: T) => x;
1112
var r2 = < <T>(x: T) => T>f; // valid
@@ -20,4 +21,6 @@ ambiguousGenericAssertion1.ts(4,21): error TS1005: ';' expected.
2021
!!! error TS1005: ',' expected.
2122
~~
2223
!!! error TS1005: ';' expected.
24+
~~~
25+
!!! error TS2365: Operator '>' cannot be applied to types 'any' and '<T>(x: T) => T'.
2326

tests/baselines/reference/comparisonOperatorWithIdenticalObjects.errors.txt

+365
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
comparisonOperatorWithIdenticalTypeParameter.ts(2,14): error TS2365: Operator '<' cannot be applied to types 'T' and 'T'.
2+
comparisonOperatorWithIdenticalTypeParameter.ts(3,14): error TS2365: Operator '>' cannot be applied to types 'T' and 'T'.
3+
comparisonOperatorWithIdenticalTypeParameter.ts(4,14): error TS2365: Operator '<=' cannot be applied to types 'T' and 'T'.
4+
comparisonOperatorWithIdenticalTypeParameter.ts(5,14): error TS2365: Operator '>=' cannot be applied to types 'T' and 'T'.
5+
6+
7+
==== comparisonOperatorWithIdenticalTypeParameter.ts (4 errors) ====
8+
function foo<T>(t: T) {
9+
var r1 = t < t;
10+
~~~~~
11+
!!! error TS2365: Operator '<' cannot be applied to types 'T' and 'T'.
12+
var r2 = t > t;
13+
~~~~~
14+
!!! error TS2365: Operator '>' cannot be applied to types 'T' and 'T'.
15+
var r3 = t <= t;
16+
~~~~~~
17+
!!! error TS2365: Operator '<=' cannot be applied to types 'T' and 'T'.
18+
var r4 = t >= t;
19+
~~~~~~
20+
!!! error TS2365: Operator '>=' cannot be applied to types 'T' and 'T'.
21+
var r5 = t == t;
22+
var r6 = t != t;
23+
var r7 = t === t;
24+
var r8 = t !== t;
25+
}

tests/baselines/reference/comparisonOperatorWithInvalidOperand.errors.txt

+13-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ comparisonOperatorWithInvalidOperand.ts(34,12): error TS2365: Operator '<' canno
1818
comparisonOperatorWithInvalidOperand.ts(35,12): error TS2365: Operator '<' cannot be applied to types 'number' and '{}'.
1919
comparisonOperatorWithInvalidOperand.ts(36,12): error TS2365: Operator '<' cannot be applied to types 'number' and 'string[]'.
2020
comparisonOperatorWithInvalidOperand.ts(38,12): error TS2365: Operator '<' cannot be applied to types 'number' and 'Number'.
21+
comparisonOperatorWithInvalidOperand.ts(45,12): error TS2365: Operator '<' cannot be applied to types 'Date' and 'Date'.
2122
comparisonOperatorWithInvalidOperand.ts(46,12): error TS2365: Operator '<' cannot be applied to types 'Date' and 'boolean'.
2223
comparisonOperatorWithInvalidOperand.ts(50,12): error TS2365: Operator '>' cannot be applied to types 'boolean' and 'boolean'.
2324
comparisonOperatorWithInvalidOperand.ts(51,12): error TS2365: Operator '>' cannot be applied to types 'boolean' and 'number'.
@@ -37,6 +38,7 @@ comparisonOperatorWithInvalidOperand.ts(66,12): error TS2365: Operator '>' canno
3738
comparisonOperatorWithInvalidOperand.ts(67,12): error TS2365: Operator '>' cannot be applied to types 'number' and '{}'.
3839
comparisonOperatorWithInvalidOperand.ts(68,12): error TS2365: Operator '>' cannot be applied to types 'number' and 'string[]'.
3940
comparisonOperatorWithInvalidOperand.ts(70,12): error TS2365: Operator '>' cannot be applied to types 'number' and 'Number'.
41+
comparisonOperatorWithInvalidOperand.ts(77,12): error TS2365: Operator '>' cannot be applied to types 'Date' and 'Date'.
4042
comparisonOperatorWithInvalidOperand.ts(78,12): error TS2365: Operator '>' cannot be applied to types 'Date' and 'boolean'.
4143
comparisonOperatorWithInvalidOperand.ts(82,12): error TS2365: Operator '<=' cannot be applied to types 'boolean' and 'boolean'.
4244
comparisonOperatorWithInvalidOperand.ts(83,12): error TS2365: Operator '<=' cannot be applied to types 'boolean' and 'number'.
@@ -56,6 +58,7 @@ comparisonOperatorWithInvalidOperand.ts(98,12): error TS2365: Operator '<=' cann
5658
comparisonOperatorWithInvalidOperand.ts(99,12): error TS2365: Operator '<=' cannot be applied to types 'number' and '{}'.
5759
comparisonOperatorWithInvalidOperand.ts(100,12): error TS2365: Operator '<=' cannot be applied to types 'number' and 'string[]'.
5860
comparisonOperatorWithInvalidOperand.ts(102,12): error TS2365: Operator '<=' cannot be applied to types 'number' and 'Number'.
61+
comparisonOperatorWithInvalidOperand.ts(109,12): error TS2365: Operator '<=' cannot be applied to types 'Date' and 'Date'.
5962
comparisonOperatorWithInvalidOperand.ts(110,12): error TS2365: Operator '<=' cannot be applied to types 'Date' and 'boolean'.
6063
comparisonOperatorWithInvalidOperand.ts(114,12): error TS2365: Operator '>=' cannot be applied to types 'boolean' and 'boolean'.
6164
comparisonOperatorWithInvalidOperand.ts(115,12): error TS2365: Operator '>=' cannot be applied to types 'boolean' and 'number'.
@@ -75,10 +78,11 @@ comparisonOperatorWithInvalidOperand.ts(130,12): error TS2365: Operator '>=' can
7578
comparisonOperatorWithInvalidOperand.ts(131,12): error TS2365: Operator '>=' cannot be applied to types 'number' and '{}'.
7679
comparisonOperatorWithInvalidOperand.ts(132,12): error TS2365: Operator '>=' cannot be applied to types 'number' and 'string[]'.
7780
comparisonOperatorWithInvalidOperand.ts(134,12): error TS2365: Operator '>=' cannot be applied to types 'number' and 'Number'.
81+
comparisonOperatorWithInvalidOperand.ts(141,12): error TS2365: Operator '>=' cannot be applied to types 'Date' and 'Date'.
7882
comparisonOperatorWithInvalidOperand.ts(142,12): error TS2365: Operator '>=' cannot be applied to types 'Date' and 'boolean'.
7983

8084

81-
==== comparisonOperatorWithInvalidOperand.ts (78 errors) ====
85+
==== comparisonOperatorWithInvalidOperand.ts (82 errors) ====
8286
// repro #15506
8387
// assumes that only valid comparisons are between anys, numbers and strings
8488
var a: boolean = false;
@@ -164,6 +168,8 @@ comparisonOperatorWithInvalidOperand.ts(142,12): error TS2365: Operator '>=' can
164168

165169
// Date
166170
var r1c1 = d < d;
171+
~~~~~
172+
!!! error TS2365: Operator '<' cannot be applied to types 'Date' and 'Date'.
167173
var r1c2 = d < a;
168174
~~~~~
169175
!!! error TS2365: Operator '<' cannot be applied to types 'Date' and 'boolean'.
@@ -234,6 +240,8 @@ comparisonOperatorWithInvalidOperand.ts(142,12): error TS2365: Operator '>=' can
234240

235241
// Date
236242
var r2c1 = d > d;
243+
~~~~~
244+
!!! error TS2365: Operator '>' cannot be applied to types 'Date' and 'Date'.
237245
var r2c2 = d > a;
238246
~~~~~
239247
!!! error TS2365: Operator '>' cannot be applied to types 'Date' and 'boolean'.
@@ -304,6 +312,8 @@ comparisonOperatorWithInvalidOperand.ts(142,12): error TS2365: Operator '>=' can
304312

305313
// Date
306314
var r3c1 = d <= d;
315+
~~~~~~
316+
!!! error TS2365: Operator '<=' cannot be applied to types 'Date' and 'Date'.
307317
var r3c2 = d <= a;
308318
~~~~~~
309319
!!! error TS2365: Operator '<=' cannot be applied to types 'Date' and 'boolean'.
@@ -374,6 +384,8 @@ comparisonOperatorWithInvalidOperand.ts(142,12): error TS2365: Operator '>=' can
374384

375385
// Date
376386
var r4c1 = d >= d;
387+
~~~~~~
388+
!!! error TS2365: Operator '>=' cannot be applied to types 'Date' and 'Date'.
377389
var r4c2 = d >= a;
378390
~~~~~~
379391
!!! error TS2365: Operator '>=' cannot be applied to types 'Date' and 'boolean'.

0 commit comments

Comments
 (0)