Skip to content

Commit 9b2a760

Browse files
committed
Cherrypick non-comparability related changes from prolific literals PR
1 parent 98112d4 commit 9b2a760

File tree

561 files changed

+3474
-2504
lines changed

Some content is hidden

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

561 files changed

+3474
-2504
lines changed

src/compiler/checker.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4306,9 +4306,10 @@ namespace ts {
43064306
if (strictNullChecks && declaration.initializer && !(getFalsyFlags(checkExpressionCached(declaration.initializer)) & TypeFlags.Undefined)) {
43074307
type = getTypeWithFacts(type, TypeFacts.NEUndefined);
43084308
}
4309-
return declaration.initializer ?
4309+
type = declaration.initializer ?
43104310
getUnionType([type, checkExpressionCached(declaration.initializer)], /*subtypeReduction*/ true) :
43114311
type;
4312+
return shouldUseLiteralType(declaration) ? type : getWidenedLiteralType(type);
43124313
}
43134314

43144315
function getTypeForDeclarationFromJSDocComment(declaration: Node) {
@@ -10672,7 +10673,7 @@ namespace ts {
1067210673
}
1067310674

1067410675
function getWidenedLiteralLikeTypeForContextualType(type: Type, contextualType: Type) {
10675-
if (!isLiteralLikeContextualType(contextualType)) {
10676+
if (!isLiteralLikeContextualType(type, contextualType)) {
1067610677
type = getWidenedUniqueESSymbolType(getWidenedLiteralType(type));
1067710678
}
1067810679
return type;
@@ -18856,24 +18857,30 @@ namespace ts {
1885618857

1885718858
function checkDeclarationInitializer(declaration: VariableLikeDeclaration) {
1885818859
const type = getTypeOfExpression(declaration.initializer, /*cache*/ true);
18860+
return shouldUseLiteralType(declaration) ? type : getWidenedLiteralType(type);
18861+
}
18862+
18863+
function shouldUseLiteralType(declaration: VariableLikeDeclaration) {
1885918864
return getCombinedNodeFlags(declaration) & NodeFlags.Const ||
18860-
(getCombinedModifierFlags(declaration) & ModifierFlags.Readonly && !isParameterPropertyDeclaration(declaration)) ||
18861-
isTypeAssertion(declaration.initializer) ? type : getWidenedLiteralType(type);
18865+
getCombinedModifierFlags(declaration) & ModifierFlags.Readonly && !isParameterPropertyDeclaration(declaration) ||
18866+
(declaration.initializer && isTypeAssertion(declaration.initializer));
1886218867
}
1886318868

18864-
function isLiteralLikeContextualType(contextualType: Type) {
18869+
function isLiteralLikeContextualType(candidateLiteral: Type, contextualType: Type): boolean {
1886518870
if (contextualType) {
18871+
if (contextualType.flags & TypeFlags.UnionOrIntersection) {
18872+
return some((contextualType as UnionOrIntersectionType).types, t => isLiteralLikeContextualType(candidateLiteral, t));
18873+
}
1886618874
if (contextualType.flags & TypeFlags.TypeVariable) {
1886718875
const constraint = getBaseConstraintOfType(contextualType) || emptyObjectType;
18868-
// If the type parameter is constrained to the base primitive type we're checking for,
18869-
// consider this a literal context. For example, given a type parameter 'T extends string',
18870-
// this causes us to infer string literal types for T.
18871-
if (constraint.flags & (TypeFlags.String | TypeFlags.Number | TypeFlags.Boolean | TypeFlags.Enum | TypeFlags.ESSymbol)) {
18872-
return true;
18873-
}
18874-
contextualType = constraint;
18875-
}
18876-
return maybeTypeOfKind(contextualType, (TypeFlags.Literal | TypeFlags.Index | TypeFlags.UniqueESSymbol));
18876+
return isLiteralLikeContextualType(candidateLiteral, constraint);
18877+
}
18878+
// No need to `maybeTypeOfKind` on the contextual type, as it can't be a union, _however_, `candidateLiteral` might still be one!
18879+
return !!(((contextualType.flags & TypeFlags.StringLike) && maybeTypeOfKind(candidateLiteral, TypeFlags.StringLike)) ||
18880+
((contextualType.flags & TypeFlags.NumberLike) && maybeTypeOfKind(candidateLiteral, TypeFlags.NumberLike)) ||
18881+
((contextualType.flags & TypeFlags.BooleanLike) && maybeTypeOfKind(candidateLiteral, TypeFlags.BooleanLike)) ||
18882+
((contextualType.flags & TypeFlags.ESSymbolLike) && maybeTypeOfKind(candidateLiteral, TypeFlags.ESSymbolLike))
18883+
);
1887718884
}
1887818885
return false;
1887918886
}

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: number; y: number; }
12+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; }
41+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; }
12+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; }
41+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; }
12+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; }
41+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; }
12+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; }
41+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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"] : [number, string]
8+
>[2, "3"] : [2, "3"]
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] : [string, number]
4+
>["", 0] : ["", 0]
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] : number[]
23+
>array = [4,5,6] : (4 | 5 | 6)[]
2424
>array : number[]
25-
>[4,5,6] : number[]
25+
>[4,5,6] : (4 | 5 | 6)[]
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: number; y: number; }
18+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; z: number; }
35+
>{ x: 0, y: 0, z: 0 } : { x: 0; y: 0; z: 0; }
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: number; y: number; }
18+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; z: number; }
35+
>{ x: 0, y: 0, z: 0 } : { x: 0; y: 0; z: 0; }
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: number; y: number; }
34+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; }
34+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; }
34+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; }
18+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; z: number; }
35+
>{ x: 0, y: 0, z: 0 } : { x: 0; y: 0; z: 0; }
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: number; y: number; }
18+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; z: number; }
35+
>{ x: 0, y: 0, z: 0 } : { x: 0; y: 0; z: 0; }
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: number; y: number; }
43+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; }
16+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; }
19+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; }
19+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; z: number; }
37+
>{ x: 0, y: 0, z: 0 } : { x: 0; y: 0; z: 0; }
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: number; y: number; }
55+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; }
71+
>{ x: 1, y: 0 } : { x: 1; y: 0; }
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: number; y: number; }
40+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; }
124+
>{ x: 1, y: 1 } : { x: 1; y: 1; }
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: number; y: number; }
41+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; }
56+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; }
46+
>{ x: 0, y: 0 } : { x: 0; y: 0; }
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: number; y: number; }
120+
>{ x: 1, y: 1 } : { x: 1; y: 1; }
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) => string
17+
>(x: string) => "" : (x: string) => ""
1818
>x : string
1919
>"" : ""
2020
}

0 commit comments

Comments
 (0)