Skip to content

Commit b08779b

Browse files
committed
Cherrypick non-comparability related changes from prolific literals PR
1 parent e7df832 commit b08779b

File tree

559 files changed

+3462
-2494
lines changed

Some content is hidden

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

559 files changed

+3462
-2494
lines changed

src/compiler/checker.ts

+18-13
Original file line numberDiff line numberDiff line change
@@ -4265,9 +4265,10 @@ namespace ts {
42654265
if (strictNullChecks && declaration.initializer && !(getFalsyFlags(checkExpressionCached(declaration.initializer)) & TypeFlags.Undefined)) {
42664266
type = getTypeWithFacts(type, TypeFacts.NEUndefined);
42674267
}
4268-
return declaration.initializer ?
4268+
type = declaration.initializer ?
42694269
getUnionType([type, checkExpressionCached(declaration.initializer)], /*subtypeReduction*/ true) :
42704270
type;
4271+
return shouldUseLiteralType(declaration) ? type : getWidenedLiteralType(type);
42714272
}
42724273

42734274
function getTypeForDeclarationFromJSDocComment(declaration: Node) {
@@ -17403,6 +17404,7 @@ namespace ts {
1740317404
if (isUnitType(type) &&
1740417405
!(contextualSignature &&
1740517406
isLiteralContextualType(
17407+
type,
1740617408
contextualSignature === getSignatureFromDeclaration(func) ? type : getReturnTypeOfSignature(contextualSignature)))) {
1740717409
type = getWidenedLiteralType(type);
1740817410
}
@@ -18487,24 +18489,27 @@ namespace ts {
1848718489

1848818490
function checkDeclarationInitializer(declaration: VariableLikeDeclaration) {
1848918491
const type = getTypeOfExpression(declaration.initializer, /*cache*/ true);
18492+
return shouldUseLiteralType(declaration) ? type : getWidenedLiteralType(type);
18493+
}
18494+
function shouldUseLiteralType(declaration: VariableLikeDeclaration) {
1849018495
return getCombinedNodeFlags(declaration) & NodeFlags.Const ||
1849118496
getCombinedModifierFlags(declaration) & ModifierFlags.Readonly && !isParameterPropertyDeclaration(declaration) ||
18492-
isTypeAssertion(declaration.initializer) ? type : getWidenedLiteralType(type);
18497+
(declaration.initializer && isTypeAssertion(declaration.initializer));
1849318498
}
1849418499

18495-
function isLiteralContextualType(contextualType: Type) {
18500+
function isLiteralContextualType(candidateLiteral: Type, contextualType: Type): boolean {
1849618501
if (contextualType) {
18502+
if (contextualType.flags & TypeFlags.UnionOrIntersection) {
18503+
return some((contextualType as UnionOrIntersectionType).types, t => isLiteralContextualType(candidateLiteral, t));
18504+
}
1849718505
if (contextualType.flags & TypeFlags.TypeVariable) {
1849818506
const constraint = getBaseConstraintOfType(contextualType) || emptyObjectType;
18499-
// If the type parameter is constrained to the base primitive type we're checking for,
18500-
// consider this a literal context. For example, given a type parameter 'T extends string',
18501-
// this causes us to infer string literal types for T.
18502-
if (constraint.flags & (TypeFlags.String | TypeFlags.Number | TypeFlags.Boolean | TypeFlags.Enum)) {
18503-
return true;
18504-
}
18505-
contextualType = constraint;
18507+
return isLiteralContextualType(candidateLiteral, constraint);
1850618508
}
18507-
return maybeTypeOfKind(contextualType, (TypeFlags.Literal | TypeFlags.Index));
18509+
// No need to `maybeTypeOfKind` on the contextual type, as it can't be a union, _however_, `candidateLiteral` might still be one!
18510+
return !!(((contextualType.flags & TypeFlags.StringLike) && maybeTypeOfKind(candidateLiteral, TypeFlags.StringLike)) ||
18511+
((contextualType.flags & TypeFlags.NumberLike) && maybeTypeOfKind(candidateLiteral, TypeFlags.NumberLike)) ||
18512+
((contextualType.flags & TypeFlags.BooleanLike) && maybeTypeOfKind(candidateLiteral, TypeFlags.BooleanLike)));
1850818513
}
1850918514
return false;
1851018515
}
@@ -18514,8 +18519,8 @@ namespace ts {
1851418519
contextualType = getContextualType(node);
1851518520
}
1851618521
const type = checkExpression(node, checkMode);
18517-
const shouldWiden = isTypeAssertion(node) || isLiteralContextualType(contextualType);
18518-
return shouldWiden ? type : getWidenedLiteralType(type);
18522+
const shouldNotWiden = isTypeAssertion(node) || isLiteralContextualType(type, contextualType);
18523+
return shouldNotWiden ? type : getWidenedLiteralType(type);
1851918524
}
1852018525

1852118526
function checkPropertyAssignment(node: PropertyAssignment, checkMode?: CheckMode): Type {

tests/baselines/reference/ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.types

+2-2
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

+2-2
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

+2-2
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

+2-2
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

+1-1
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

+1-1
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

+2-2
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

+2-2
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

+2-2
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

+1-1
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

+1-1
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

+1-1
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

+2-2
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

+2-2
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

+1-1
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

+1-1
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

+1-1
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

+2-2
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

+2-2
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

+2-2
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

+1-1
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

+1-1
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

+2-2
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

+1-1
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)