Skip to content

Commit

Permalink
Revert changes to widening on destructuring initalizers
Browse files Browse the repository at this point in the history
  • Loading branch information
weswigham committed Dec 8, 2017
1 parent a381bc8 commit e61f47d
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 25 deletions.
15 changes: 4 additions & 11 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4337,10 +4337,9 @@ namespace ts {
if (strictNullChecks && declaration.initializer && !(getFalsyFlags(checkExpressionCached(declaration.initializer)) & TypeFlags.Undefined)) {
type = getTypeWithFacts(type, TypeFacts.NEUndefined);
}
type = declaration.initializer ?
return declaration.initializer ?
getUnionType([type, checkExpressionCached(declaration.initializer)], /*subtypeReduction*/ true) :
type;
return shouldKeepLiteralType(declaration) ? type : getWidenedLiteralType(type);
}

function getTypeForDeclarationFromJSDocComment(declaration: Node) {
Expand Down Expand Up @@ -19085,16 +19084,11 @@ namespace ts {

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

function shouldKeepLiteralType(declaration: VariableLikeDeclaration) {
return getCombinedNodeFlags(declaration) & NodeFlags.Const ||
getCombinedModifierFlags(declaration) & ModifierFlags.Readonly && !isParameterPropertyDeclaration(declaration) ||
(declaration.initializer && isTypeAssertion(declaration.initializer));
}


function isLiteralOfContextualType(candidateType: Type, contextualType: Type): boolean {
if (contextualType) {
if (contextualType.flags & TypeFlags.Union && !(contextualType.flags & TypeFlags.Boolean)) {
Expand All @@ -19110,7 +19104,6 @@ namespace ts {
// this a literal context for literals of that primitive type. For example, given a
// type parameter 'T extends string', infer string literal types for T.
const constraint = getBaseConstraintOfType(contextualType) || emptyObjectType;

return constraint.flags & TypeFlags.String && maybeTypeOfKind(candidateType, TypeFlags.StringLiteral) ||
constraint.flags & TypeFlags.Number && maybeTypeOfKind(candidateType, TypeFlags.NumberLiteral) ||
constraint.flags & TypeFlags.Boolean && maybeTypeOfKind(candidateType, TypeFlags.BooleanLiteral) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ interface StringUnion {
}
function h({ prop = "baz" }: StringUnion) {}
>h : ({ prop }: StringUnion) => void
>prop : string
>prop : "foo" | "bar" | "baz"
>"baz" : "baz"
>StringUnion : StringUnion

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(23,25):
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; }'.
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; }'.
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; }'.
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'.
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'.
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.
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.
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.
Expand Down Expand Up @@ -99,7 +99,7 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,9):
var x: number;
var y: string;
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'y' must be of type 'string | number', but here has type 'string'.
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'y' must be of type 'string | 1', but here has type 'string'.
}

function f8() {
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/declarationsAndAssignments.types
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ function f7() {
var [x = 0, y = 1] = [1, "hello"]; // Error, initializer for y must be string
>x : number
>0 : 0
>y : string | number
>y : string | 1
>1 : 1
>[1, "hello"] : [number, string]
>1 : 1
Expand All @@ -248,7 +248,7 @@ function f7() {
>x : number

var y: string;
>y : string | number
>y : string | 1
}

function f8() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function bar(): J {
>3 : 3
}
var [b3 = "string", b4, b5] = bar(); // Error
>b3 : string | Number
>b3 : Number | "string"
>"string" : "string"
>b4 : Number
>b5 : Number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ var [b2 = 3, b3 = true, b4 = temp] = [3, false, { t1: false, t2: "hello" }];
>"hello" : "hello"

var [b5 = 3, b6 = true, b7 = temp] = [undefined, undefined, undefined];
>b5 : number
>b5 : 3
>3 : 3
>b6 : boolean
>b6 : true
>true : true
>b7 : { t1: boolean; t2: string; }
>temp : { t1: boolean; t2: string; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ var [b2 = 3, b3 = true, b4 = temp] = [3, false, { t1: false, t2: "hello" }];
>"hello" : "hello"

var [b5 = 3, b6 = true, b7 = temp] = [undefined, undefined, undefined];
>b5 : number
>b5 : 3
>3 : 3
>b6 : boolean
>b6 : true
>true : true
>b7 : { t1: boolean; t2: string; }
>temp : { t1: boolean; t2: string; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ var [b2 = 3, b3 = true, b4 = temp] = [3, false, { t1: false, t2: "hello" }];
>"hello" : "hello"

var [b5 = 3, b6 = true, b7 = temp] = [undefined, undefined, undefined];
>b5 : number
>b5 : 3
>3 : 3
>b6 : boolean
>b6 : true
>true : true
>b7 : { t1: boolean; t2: string; }
>temp : { t1: boolean; t2: string; }
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/for-of43.types
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ for (var {x: a = "", y: b = true} of array) {
>a : string
>"" : ""
>y : any
>b : number | boolean
>b : number | true
>true : true
>array : { x: string; y: number; }[]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ let { b = "foo" as "foo" } = { b: "bar" };
>"bar" : "bar"

let { c = "foo" } = { c: "bar" as "bar" };
>c : string
>c : "foo" | "bar"
>"foo" : "foo"
>{ c: "bar" as "bar" } : { c?: "bar"; }
>c : "bar"
Expand Down

0 comments on commit e61f47d

Please sign in to comment.