Skip to content

Commit 12ce050

Browse files
committed
Exclude the domain root from contextual typing literals except for type variables
1 parent ba34d9d commit 12ce050

File tree

549 files changed

+2608
-2479
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

549 files changed

+2608
-2479
lines changed

src/compiler/checker.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18908,13 +18908,20 @@ namespace ts {
1890818908
}
1890918909
if (contextualType.flags & TypeFlags.TypeVariable) {
1891018910
const constraint = getBaseConstraintOfType(contextualType) || emptyObjectType;
18911-
return isLiteralOfContextualType(candidateLiteral, constraint);
18911+
// A constraint of a literal domain causes the inference of literal types, ie `T extends string` contextually typing a `"foo"` causes `"foo"` to be the type
18912+
return !!((constraint.flags & TypeFlags.String && maybeTypeOfKind(candidateLiteral, TypeFlags.StringLike)) ||
18913+
(constraint.flags & TypeFlags.Number && maybeTypeOfKind(candidateLiteral, TypeFlags.NumberLike)) ||
18914+
(constraint.flags & TypeFlags.Boolean && maybeTypeOfKind(candidateLiteral, TypeFlags.BooleanLike)) ||
18915+
(constraint.flags & TypeFlags.ESSymbol && maybeTypeOfKind(candidateLiteral, TypeFlags.ESSymbolLike)) ||
18916+
isLiteralOfContextualType(candidateLiteral, constraint)
18917+
);
1891218918
}
1891318919
// No need to `maybeTypeOfKind` on the contextual type, as it can't be a union, _however_, `candidateLiteral` might still be one!
18914-
return !!((contextualType.flags & TypeFlags.StringLike && maybeTypeOfKind(candidateLiteral, TypeFlags.StringLike)) ||
18915-
(contextualType.flags & TypeFlags.NumberLike && maybeTypeOfKind(candidateLiteral, TypeFlags.NumberLike)) ||
18916-
(contextualType.flags & TypeFlags.BooleanLike && maybeTypeOfKind(candidateLiteral, TypeFlags.BooleanLike)) ||
18917-
(contextualType.flags & TypeFlags.ESSymbolLike && maybeTypeOfKind(candidateLiteral, TypeFlags.ESSymbolLike))
18920+
// Please note: Only another literal in the same domain _and not the domain itself_ causes a literal type to be produced
18921+
return !!((contextualType.flags & TypeFlags.StringLike && !(contextualType.flags & TypeFlags.String) && maybeTypeOfKind(candidateLiteral, TypeFlags.StringLike)) ||
18922+
(contextualType.flags & TypeFlags.NumberLike && !(contextualType.flags & TypeFlags.Number) && maybeTypeOfKind(candidateLiteral, TypeFlags.NumberLike)) ||
18923+
(contextualType.flags & TypeFlags.BooleanLike && !(contextualType.flags & TypeFlags.Boolean) && maybeTypeOfKind(candidateLiteral, TypeFlags.BooleanLike)) ||
18924+
(contextualType.flags & TypeFlags.ESSymbolLike && !(contextualType.flags & TypeFlags.ESSymbol) && maybeTypeOfKind(candidateLiteral, TypeFlags.ESSymbolLike))
1891818925
);
1891918926
}
1892018927
return false;

tests/baselines/reference/ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Point {
99
static Origin(): Point { return { x: 0, y: 0 }; } // unexpected error here bug 840246
1010
>Origin : () => Point
1111
>Point : Point
12-
>{ x: 0, y: 0 } : { x: 0; y: 0; }
12+
>{ x: 0, y: 0 } : { x: number; y: number; }
1313
>x : number
1414
>0 : 0
1515
>y : number
@@ -38,7 +38,7 @@ module A {
3838
static Origin(): Point { return { x: 0, y: 0 }; } // unexpected error here bug 840246
3939
>Origin : () => Point
4040
>Point : Point
41-
>{ x: 0, y: 0 } : { x: 0; y: 0; }
41+
>{ x: 0, y: 0 } : { x: number; y: number; }
4242
>x : number
4343
>0 : 0
4444
>y : number

tests/baselines/reference/ClassAndModuleThatMergeWithStaticFunctionAndNonExportedFunctionThatShareAName.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Point {
99
static Origin(): Point { return { x: 0, y: 0 }; }
1010
>Origin : () => Point
1111
>Point : Point
12-
>{ x: 0, y: 0 } : { x: 0; y: 0; }
12+
>{ x: 0, y: 0 } : { x: number; y: number; }
1313
>x : number
1414
>0 : 0
1515
>y : number
@@ -38,7 +38,7 @@ module A {
3838
static Origin(): Point { return { x: 0, y: 0 }; }
3939
>Origin : () => Point
4040
>Point : Point
41-
>{ x: 0, y: 0 } : { x: 0; y: 0; }
41+
>{ x: 0, y: 0 } : { x: number; y: number; }
4242
>x : number
4343
>0 : 0
4444
>y : number

tests/baselines/reference/ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Point {
99
static Origin: Point = { x: 0, y: 0 };
1010
>Origin : Point
1111
>Point : Point
12-
>{ x: 0, y: 0 } : { x: 0; y: 0; }
12+
>{ x: 0, y: 0 } : { x: number; y: number; }
1313
>x : number
1414
>0 : 0
1515
>y : number
@@ -38,7 +38,7 @@ module A {
3838
static Origin: Point = { x: 0, y: 0 };
3939
>Origin : Point
4040
>Point : Point
41-
>{ x: 0, y: 0 } : { x: 0; y: 0; }
41+
>{ x: 0, y: 0 } : { x: number; y: number; }
4242
>x : number
4343
>0 : 0
4444
>y : number

tests/baselines/reference/ClassAndModuleThatMergeWithStaticVariableAndNonExportedVarThatShareAName.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Point {
99
static Origin: Point = { x: 0, y: 0 };
1010
>Origin : Point
1111
>Point : Point
12-
>{ x: 0, y: 0 } : { x: 0; y: 0; }
12+
>{ x: 0, y: 0 } : { x: number; y: number; }
1313
>x : number
1414
>0 : 0
1515
>y : number
@@ -38,7 +38,7 @@ module A {
3838
static Origin: Point = { x: 0, y: 0 };
3939
>Origin : Point
4040
>Point : Point
41-
>{ x: 0, y: 0 } : { x: 0; y: 0; }
41+
>{ x: 0, y: 0 } : { x: number; y: number; }
4242
>x : number
4343
>0 : 0
4444
>y : number

tests/baselines/reference/ES5For-of30.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var a: string, b: number;
55

66
var tuple: [number, string] = [2, "3"];
77
>tuple : [number, string]
8-
>[2, "3"] : [2, "3"]
8+
>[2, "3"] : [number, string]
99
>2 : 2
1010
>"3" : "3"
1111

tests/baselines/reference/ES5For-ofTypeCheck3.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
=== tests/cases/conformance/statements/for-ofStatements/ES5For-ofTypeCheck3.ts ===
22
var tuple: [string, number] = ["", 0];
33
>tuple : [string, number]
4-
>["", 0] : ["", 0]
4+
>["", 0] : [string, number]
55
>"" : ""
66
>0 : 0
77

tests/baselines/reference/ES5for-of32.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ for (let num of array) {
2020
>0 : 0
2121

2222
array = [4,5,6]
23-
>array = [4,5,6] : (4 | 5 | 6)[]
23+
>array = [4,5,6] : number[]
2424
>array : number[]
25-
>[4,5,6] : (4 | 5 | 6)[]
25+
>[4,5,6] : number[]
2626
>4 : 4
2727
>5 : 5
2828
>6 : 6

tests/baselines/reference/ExportClassWithAccessibleTypesInTypeParameterConstraintsClassHeritageListMemberTypeAnnotations.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module A {
1515
export var Origin: Point = { x: 0, y: 0 };
1616
>Origin : Point
1717
>Point : Point
18-
>{ x: 0, y: 0 } : { x: 0; y: 0; }
18+
>{ x: 0, y: 0 } : { x: number; y: number; }
1919
>x : number
2020
>0 : 0
2121
>y : number
@@ -32,7 +32,7 @@ module A {
3232
export var Origin3d: Point3d = { x: 0, y: 0, z: 0 };
3333
>Origin3d : Point3d
3434
>Point3d : Point3d
35-
>{ x: 0, y: 0, z: 0 } : { x: 0; y: 0; z: 0; }
35+
>{ x: 0, y: 0, z: 0 } : { x: number; y: number; z: number; }
3636
>x : number
3737
>0 : 0
3838
>y : number

tests/baselines/reference/ExportClassWithInaccessibleTypeInTypeParameterConstraint.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module A {
1515
export var Origin: Point = { x: 0, y: 0 };
1616
>Origin : Point
1717
>Point : Point
18-
>{ x: 0, y: 0 } : { x: 0; y: 0; }
18+
>{ x: 0, y: 0 } : { x: number; y: number; }
1919
>x : number
2020
>0 : 0
2121
>y : number
@@ -32,7 +32,7 @@ module A {
3232
export var Origin3d: Point3d = { x: 0, y: 0, z: 0 };
3333
>Origin3d : Point3d
3434
>Point3d : Point3d
35-
>{ x: 0, y: 0, z: 0 } : { x: 0; y: 0; z: 0; }
35+
>{ x: 0, y: 0, z: 0 } : { x: number; y: number; z: number; }
3636
>x : number
3737
>0 : 0
3838
>y : number

0 commit comments

Comments
 (0)