Skip to content

Commit e61f47d

Browse files
committed
Revert changes to widening on destructuring initalizers
1 parent a381bc8 commit e61f47d

10 files changed

+18
-25
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4337,10 +4337,9 @@ namespace ts {
43374337
if (strictNullChecks && declaration.initializer && !(getFalsyFlags(checkExpressionCached(declaration.initializer)) & TypeFlags.Undefined)) {
43384338
type = getTypeWithFacts(type, TypeFacts.NEUndefined);
43394339
}
4340-
type = declaration.initializer ?
4340+
return declaration.initializer ?
43414341
getUnionType([type, checkExpressionCached(declaration.initializer)], /*subtypeReduction*/ true) :
43424342
type;
4343-
return shouldKeepLiteralType(declaration) ? type : getWidenedLiteralType(type);
43444343
}
43454344

43464345
function getTypeForDeclarationFromJSDocComment(declaration: Node) {
@@ -19085,16 +19084,11 @@ namespace ts {
1908519084

1908619085
function checkDeclarationInitializer(declaration: VariableLikeDeclaration) {
1908719086
const type = getTypeOfExpression(declaration.initializer, /*cache*/ true);
19088-
return shouldKeepLiteralType(declaration) ? type : getWidenedLiteralType(type);
19087+
return (getCombinedNodeFlags(declaration) & NodeFlags.Const ||
19088+
getCombinedModifierFlags(declaration) & ModifierFlags.Readonly && !isParameterPropertyDeclaration(declaration)) ||
19089+
isTypeAssertion(declaration.initializer) ? type : getWidenedLiteralType(type);
1908919090
}
1909019091

19091-
function shouldKeepLiteralType(declaration: VariableLikeDeclaration) {
19092-
return getCombinedNodeFlags(declaration) & NodeFlags.Const ||
19093-
getCombinedModifierFlags(declaration) & ModifierFlags.Readonly && !isParameterPropertyDeclaration(declaration) ||
19094-
(declaration.initializer && isTypeAssertion(declaration.initializer));
19095-
}
19096-
19097-
1909819092
function isLiteralOfContextualType(candidateType: Type, contextualType: Type): boolean {
1909919093
if (contextualType) {
1910019094
if (contextualType.flags & TypeFlags.Union && !(contextualType.flags & TypeFlags.Boolean)) {
@@ -19110,7 +19104,6 @@ namespace ts {
1911019104
// this a literal context for literals of that primitive type. For example, given a
1911119105
// type parameter 'T extends string', infer string literal types for T.
1911219106
const constraint = getBaseConstraintOfType(contextualType) || emptyObjectType;
19113-
1911419107
return constraint.flags & TypeFlags.String && maybeTypeOfKind(candidateType, TypeFlags.StringLiteral) ||
1911519108
constraint.flags & TypeFlags.Number && maybeTypeOfKind(candidateType, TypeFlags.NumberLiteral) ||
1911619109
constraint.flags & TypeFlags.Boolean && maybeTypeOfKind(candidateType, TypeFlags.BooleanLiteral) ||

tests/baselines/reference/contextuallyTypedBindingInitializerNegative.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ interface StringUnion {
9494
}
9595
function h({ prop = "baz" }: StringUnion) {}
9696
>h : ({ prop }: StringUnion) => void
97-
>prop : string
97+
>prop : "foo" | "bar" | "baz"
9898
>"baz" : "baz"
9999
>StringUnion : StringUnion
100100

tests/baselines/reference/declarationsAndAssignments.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(23,25):
55
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(24,19): error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{ y: any; }'.
66
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(28,28): error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{ x: any; }'.
77
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(29,22): error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{ y: any; }'.
8-
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(58,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'y' must be of type 'string | number', but here has type 'string'.
8+
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(58,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'y' must be of type 'string | 1', but here has type 'string'.
99
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(62,10): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
1010
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(62,13): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
1111
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(62,16): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
@@ -99,7 +99,7 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,9):
9999
var x: number;
100100
var y: string;
101101
~
102-
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'y' must be of type 'string | number', but here has type 'string'.
102+
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'y' must be of type 'string | 1', but here has type 'string'.
103103
}
104104

105105
function f8() {

tests/baselines/reference/declarationsAndAssignments.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ function f7() {
238238
var [x = 0, y = 1] = [1, "hello"]; // Error, initializer for y must be string
239239
>x : number
240240
>0 : 0
241-
>y : string | number
241+
>y : string | 1
242242
>1 : 1
243243
>[1, "hello"] : [number, string]
244244
>1 : 1
@@ -248,7 +248,7 @@ function f7() {
248248
>x : number
249249

250250
var y: string;
251-
>y : string | number
251+
>y : string | 1
252252
}
253253

254254
function f8() {

tests/baselines/reference/destructuringArrayBindingPatternAndAssignment2.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function bar(): J {
4343
>3 : 3
4444
}
4545
var [b3 = "string", b4, b5] = bar(); // Error
46-
>b3 : string | Number
46+
>b3 : Number | "string"
4747
>"string" : "string"
4848
>b4 : Number
4949
>b5 : Number

tests/baselines/reference/destructuringVariableDeclaration1ES5.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ var [b2 = 3, b3 = true, b4 = temp] = [3, false, { t1: false, t2: "hello" }];
6262
>"hello" : "hello"
6363

6464
var [b5 = 3, b6 = true, b7 = temp] = [undefined, undefined, undefined];
65-
>b5 : number
65+
>b5 : 3
6666
>3 : 3
67-
>b6 : boolean
67+
>b6 : true
6868
>true : true
6969
>b7 : { t1: boolean; t2: string; }
7070
>temp : { t1: boolean; t2: string; }

tests/baselines/reference/destructuringVariableDeclaration1ES5iterable.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ var [b2 = 3, b3 = true, b4 = temp] = [3, false, { t1: false, t2: "hello" }];
6262
>"hello" : "hello"
6363

6464
var [b5 = 3, b6 = true, b7 = temp] = [undefined, undefined, undefined];
65-
>b5 : number
65+
>b5 : 3
6666
>3 : 3
67-
>b6 : boolean
67+
>b6 : true
6868
>true : true
6969
>b7 : { t1: boolean; t2: string; }
7070
>temp : { t1: boolean; t2: string; }

tests/baselines/reference/destructuringVariableDeclaration1ES6.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ var [b2 = 3, b3 = true, b4 = temp] = [3, false, { t1: false, t2: "hello" }];
6262
>"hello" : "hello"
6363

6464
var [b5 = 3, b6 = true, b7 = temp] = [undefined, undefined, undefined];
65-
>b5 : number
65+
>b5 : 3
6666
>3 : 3
67-
>b6 : boolean
67+
>b6 : true
6868
>true : true
6969
>b7 : { t1: boolean; t2: string; }
7070
>temp : { t1: boolean; t2: string; }

tests/baselines/reference/for-of43.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ for (var {x: a = "", y: b = true} of array) {
1313
>a : string
1414
>"" : ""
1515
>y : any
16-
>b : number | boolean
16+
>b : number | true
1717
>true : true
1818
>array : { x: string; y: number; }[]
1919

tests/baselines/reference/literalTypesAndTypeAssertions.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ let { b = "foo" as "foo" } = { b: "bar" };
4444
>"bar" : "bar"
4545

4646
let { c = "foo" } = { c: "bar" as "bar" };
47-
>c : string
47+
>c : "foo" | "bar"
4848
>"foo" : "foo"
4949
>{ c: "bar" as "bar" } : { c?: "bar"; }
5050
>c : "bar"

0 commit comments

Comments
 (0)