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

tests/baselines/reference/ExportFunctionWithAccessibleTypesInParameterAndReturnTypeAnnotation.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module A {
3131
return new Line({ x: 0, y: 0 }, p);
3232
>new Line({ x: 0, y: 0 }, p) : Line
3333
>Line : typeof Line
34-
>{ x: 0, y: 0 } : { x: 0; y: 0; }
34+
>{ x: 0, y: 0 } : { x: number; y: number; }
3535
>x : number
3636
>0 : 0
3737
>y : number

tests/baselines/reference/ExportFunctionWithInaccessibleTypesInParameterTypeAnnotation.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module A {
3131
return new Line({ x: 0, y: 0 }, p);
3232
>new Line({ x: 0, y: 0 }, p) : Line
3333
>Line : typeof Line
34-
>{ x: 0, y: 0 } : { x: 0; y: 0; }
34+
>{ x: 0, y: 0 } : { x: number; y: number; }
3535
>x : number
3636
>0 : 0
3737
>y : number

tests/baselines/reference/ExportFunctionWithInaccessibleTypesInReturnTypeAnnotation.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module A {
3131
return new Line({ x: 0, y: 0 }, p);
3232
>new Line({ x: 0, y: 0 }, p) : Line
3333
>Line : typeof Line
34-
>{ x: 0, y: 0 } : { x: 0; y: 0; }
34+
>{ x: 0, y: 0 } : { x: number; y: number; }
3535
>x : number
3636
>0 : 0
3737
>y : number

tests/baselines/reference/ExportInterfaceWithAccessibleTypesInTypeParameterConstraintsClassHeritageListMemberTypeAnnotations.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/ExportInterfaceWithInaccessibleTypeInTypeParameterConstraint.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/ExportModuleWithAccessibleTypesOnItsExportedMembers.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module A {
4040
return new Line({ x: 0, y: 0 }, p);
4141
>new Line({ x: 0, y: 0 }, p) : Line
4242
>Line : typeof Line
43-
>{ x: 0, y: 0 } : { x: 0; y: 0; }
43+
>{ x: 0, y: 0 } : { x: number; y: number; }
4444
>x : number
4545
>0 : 0
4646
>y : number

tests/baselines/reference/ExportObjectLiteralAndObjectTypeLiteralWithAccessibleTypesInMemberTypeAnnotations.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module A {
1313
export var Origin: Point = { x: 0, y: 0 };
1414
>Origin : Point
1515
>Point : Point
16-
>{ x: 0, y: 0 } : { x: 0; y: 0; }
16+
>{ x: 0, y: 0 } : { x: number; y: number; }
1717
>x : number
1818
>0 : 0
1919
>y : number

tests/baselines/reference/ExportVariableWithAccessibleTypeInTypeAnnotation.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module A {
1616
export var Origin: Point = { x: 0, y: 0 };
1717
>Origin : Point
1818
>Point : Point
19-
>{ x: 0, y: 0 } : { x: 0; y: 0; }
19+
>{ x: 0, y: 0 } : { x: number; y: number; }
2020
>x : number
2121
>0 : 0
2222
>y : number

tests/baselines/reference/ExportVariableWithInaccessibleTypeInTypeAnnotation.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module A {
1616
export var Origin: Point = { x: 0, y: 0 };
1717
>Origin : Point
1818
>Point : Point
19-
>{ x: 0, y: 0 } : { x: 0; y: 0; }
19+
>{ x: 0, y: 0 } : { x: number; y: number; }
2020
>x : number
2121
>0 : 0
2222
>y : number
@@ -34,7 +34,7 @@ module A {
3434
export var Origin3d: Point3d = { x: 0, y: 0, z: 0 };
3535
>Origin3d : Point3d
3636
>Point3d : Point3d
37-
>{ x: 0, y: 0, z: 0 } : { x: 0; y: 0; z: 0; }
37+
>{ x: 0, y: 0, z: 0 } : { x: number; y: number; z: number; }
3838
>x : number
3939
>0 : 0
4040
>y : number

tests/baselines/reference/ModuleWithExportedAndNonExportedImportAlias.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ module Geometry {
5252
>Origin : Points.Point
5353
>Points : any
5454
>Point : Points.Point
55-
>{ x: 0, y: 0 } : { x: 0; y: 0; }
55+
>{ x: 0, y: 0 } : { x: number; y: number; }
5656
>x : number
5757
>0 : 0
5858
>y : number
@@ -68,7 +68,7 @@ module Geometry {
6868
>Lines : typeof Lines
6969
>Line : typeof Lines.Line
7070
>Origin : Points.Point
71-
>{ x: 1, y: 0 } : { x: 1; y: 0; }
71+
>{ x: 1, y: 0 } : { x: number; y: number; }
7272
>x : number
7373
>1 : 1
7474
>y : number

tests/baselines/reference/TwoInternalModulesThatMergeEachWithExportedAndNonExportedLocalVarsOfTheSameName.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module A {
3737
export var Origin: Point = { x: 0, y: 0 };
3838
>Origin : Point
3939
>Point : Point
40-
>{ x: 0, y: 0 } : { x: 0; y: 0; }
40+
>{ x: 0, y: 0 } : { x: number; y: number; }
4141
>x : number
4242
>0 : 0
4343
>y : number
@@ -121,7 +121,7 @@ var p = new A.Utils.Plane(o, { x: 1, y: 1 });
121121
>Utils : typeof A.Utils
122122
>Plane : typeof A.Utils.Plane
123123
>o : { x: number; y: number; }
124-
>{ x: 1, y: 1 } : { x: 1; y: 1; }
124+
>{ x: 1, y: 1 } : { x: number; y: number; }
125125
>x : number
126126
>1 : 1
127127
>y : number

tests/baselines/reference/TwoInternalModulesThatMergeEachWithExportedLocalVarsOfTheSameName.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export module A {
3838
export var 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/TwoInternalModulesWithTheSameNameAndDifferentCommonRoot.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ module otherRoot {
5353
>Root : any
5454
>A : any
5555
>Point : Root.A.Point
56-
>{ x: 0, y: 0 } : { x: 0; y: 0; }
56+
>{ x: 0, y: 0 } : { x: number; y: number; }
5757
>x : number
5858
>0 : 0
5959
>y : number

tests/baselines/reference/TwoInternalModulesWithTheSameNameAndSameCommonRoot.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module A {
4343
export var Origin: Point = { x: 0, y: 0 };
4444
>Origin : Point
4545
>Point : Point
46-
>{ x: 0, y: 0 } : { x: 0; y: 0; }
46+
>{ x: 0, y: 0 } : { x: number; y: number; }
4747
>x : number
4848
>0 : 0
4949
>y : number
@@ -117,7 +117,7 @@ var p = new A.Utils.Plane(o, { x: 1, y: 1 });
117117
>Utils : typeof A.Utils
118118
>Plane : typeof A.Utils.Plane
119119
>o : { x: number; y: number; }
120-
>{ x: 1, y: 1 } : { x: 1; y: 1; }
120+
>{ x: 1, y: 1 } : { x: number; y: number; }
121121
>x : number
122122
>1 : 1
123123
>y : number

tests/baselines/reference/accessorsAreNotContextuallyTyped.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class C {
1414
>x : (a: string) => string
1515

1616
return (x: string) => "";
17-
>(x: string) => "" : (x: string) => ""
17+
>(x: string) => "" : (x: string) => string
1818
>x : string
1919
>"" : ""
2020
}

tests/baselines/reference/allowImportClausesToMergeWithTypes.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import zzz from "./b";
2020
const x: zzz = { x: "" };
2121
>x : zzz
2222
>zzz : zzz
23-
>{ x: "" } : { x: ""; }
23+
>{ x: "" } : { x: string; }
2424
>x : string
2525
>"" : ""
2626

@@ -38,7 +38,7 @@ import zzz from "./a";
3838
const x: zzz = { x: "" };
3939
>x : zzz
4040
>zzz : zzz
41-
>{ x: "" } : { x: ""; }
41+
>{ x: "" } : { x: string; }
4242
>x : string
4343
>"" : ""
4444

0 commit comments

Comments
 (0)