Skip to content

Commit e9ad5da

Browse files
committed
Match number and string literal types to number and string in inference
1 parent ab98dcf commit e9ad5da

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7608,11 +7608,18 @@ namespace ts {
76087608
// itself. When inferring from a type to itself we effectively find all type parameter
76097609
// occurrences within that type and infer themselves as their type arguments.
76107610
let matchingTypes: Type[];
7611-
for (const t of (<UnionOrIntersectionType>target).types) {
7612-
if (typeIdenticalToSomeType(t, (<UnionOrIntersectionType>source).types)) {
7611+
for (const t of (<UnionOrIntersectionType>source).types) {
7612+
if (typeIdenticalToSomeType(t, (<UnionOrIntersectionType>target).types)) {
76137613
(matchingTypes || (matchingTypes = [])).push(t);
76147614
inferFromTypes(t, t);
76157615
}
7616+
else if (t.flags & (TypeFlags.NumberLiteral | TypeFlags.StringLiteral)) {
7617+
const b = getBaseTypeOfLiteralType(t);
7618+
if (typeIdenticalToSomeType(b, (<UnionOrIntersectionType>target).types)) {
7619+
(matchingTypes || (matchingTypes = [])).push(t);
7620+
matchingTypes.push(b);
7621+
}
7622+
}
76167623
}
76177624
// Next, to improve the quality of inferences, reduce the source and target types by
76187625
// removing the identically matched constituents. For example, when inferring from

0 commit comments

Comments
 (0)