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

Lines changed: 18 additions & 13 deletions
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

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

0 commit comments

Comments
 (0)