Skip to content

Commit 3622e9a

Browse files
author
Andy Hanson
committed
Handle literals and literal unions
1 parent b36e5de commit 3622e9a

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/compiler/checker.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21997,23 +21997,29 @@ namespace ts {
2199721997
readonly side: "left" | "right";
2199821998
readonly conversionFunctionName: string;
2199921999
}
22000+
2200022001
function getExplicitConversion(left: Type, right: Type): ExplicitConversion | undefined {
22001-
const convertLeft = getFunctionNameToConvertToType(right);
22002+
const convertLeft = getFunctionNameToConvertToType(right, left);
2200222003
if (convertLeft) return { side: "left", conversionFunctionName: convertLeft };
22003-
const convertRight = getFunctionNameToConvertToType(left);
22004+
const convertRight = getFunctionNameToConvertToType(left, right);
2200422005
if (convertRight) return { side: "right", conversionFunctionName: convertRight };
2200522006
return undefined;
2200622007
}
22007-
function getFunctionNameToConvertToType(type: Type): string | undefined {
22008-
return type.flags & TypeFlags.String
22008+
22009+
function getFunctionNameToConvertToType(to: Type, from: Type): string | undefined {
22010+
return typeOrTypesHaveFlags(to, TypeFlags.StringLike) && !typeOrTypesHaveFlags(from, TypeFlags.StringLike)
2200922011
? "String"
22010-
: type.flags & TypeFlags.Number
22012+
: typeOrTypesHaveFlags(to, TypeFlags.NumberLike) && !typeOrTypesHaveFlags(from, TypeFlags.NumberLike)
2201122013
? "Number"
22012-
: type.flags & TypeFlags.Boolean
22014+
: typeOrTypesHaveFlags(to, TypeFlags.BooleanLike) && !typeOrTypesHaveFlags(from, TypeFlags.BooleanLike)
2201322015
? "Boolean"
2201422016
: undefined;
2201522017
}
2201622018

22019+
function typeOrTypesHaveFlags(type: Type, flags: TypeFlags): boolean {
22020+
return (type.flags & TypeFlags.Union ? (type as UnionType).types : [type]).every(t => !!(t.flags & flags));
22021+
}
22022+
2201722023
function isYieldExpressionInClass(node: YieldExpression): boolean {
2201822024
let current: Node = node;
2201922025
let parent = node.parent;

tests/baselines/reference/expr.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ tests/cases/compiler/expr.ts(87,5): error TS2587: Types 'number' and 'string' ha
22
tests/cases/compiler/expr.ts(88,5): error TS2587: Types 'number' and 'boolean' have no overlap. Consider explicitly converting the left side using `Boolean(x)`.
33
tests/cases/compiler/expr.ts(94,5): error TS2587: Types 'string' and 'number' have no overlap. Consider explicitly converting the left side using `Number(x)`.
44
tests/cases/compiler/expr.ts(95,5): error TS2587: Types 'string' and 'boolean' have no overlap. Consider explicitly converting the left side using `Boolean(x)`.
5-
tests/cases/compiler/expr.ts(98,5): error TS2588: Types 'string' and 'E' have no overlap. Consider explicitly converting the right side using `String(x)`.
5+
tests/cases/compiler/expr.ts(98,5): error TS2587: Types 'string' and 'E' have no overlap. Consider explicitly converting the left side using `Number(x)`.
66
tests/cases/compiler/expr.ts(115,5): error TS2587: Types 'E' and 'string' have no overlap. Consider explicitly converting the left side using `String(x)`.
7-
tests/cases/compiler/expr.ts(116,5): error TS2367: This condition will always return 'false' since the types 'E' and 'false' have no overlap.
7+
tests/cases/compiler/expr.ts(116,5): error TS2587: Types 'E' and 'false' have no overlap. Consider explicitly converting the left side using `Boolean(x)`.
88
tests/cases/compiler/expr.ts(142,5): error TS2365: Operator '+' cannot be applied to types 'number' and 'false'.
99
tests/cases/compiler/expr.ts(143,5): error TS2365: Operator '+' cannot be applied to types 'number' and 'I'.
1010
tests/cases/compiler/expr.ts(161,5): error TS2365: Operator '+' cannot be applied to types 'I' and 'number'.
@@ -177,7 +177,7 @@ tests/cases/compiler/expr.ts(242,7): error TS2363: The right-hand side of an ari
177177
s==s;
178178
s==e;
179179
~~~~
180-
!!! error TS2588: Types 'string' and 'E' have no overlap. Consider explicitly converting the right side using `String(x)`.
180+
!!! error TS2587: Types 'string' and 'E' have no overlap. Consider explicitly converting the left side using `Number(x)`.
181181

182182
a==n;
183183
a==s;
@@ -199,7 +199,7 @@ tests/cases/compiler/expr.ts(242,7): error TS2363: The right-hand side of an ari
199199
!!! error TS2587: Types 'E' and 'string' have no overlap. Consider explicitly converting the left side using `String(x)`.
200200
e==b;
201201
~~~~
202-
!!! error TS2367: This condition will always return 'false' since the types 'E' and 'false' have no overlap.
202+
!!! error TS2587: Types 'E' and 'false' have no overlap. Consider explicitly converting the left side using `Boolean(x)`.
203203
e==a;
204204
e==i;
205205
e==e;

0 commit comments

Comments
 (0)