Skip to content

Fix widening in destructuring #22675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4310,12 +4310,12 @@ namespace ts {
function getTypeFromObjectBindingPattern(pattern: ObjectBindingPattern, includePatternInType: boolean, reportErrors: boolean): Type {
const members = createSymbolTable();
let stringIndexInfo: IndexInfo;
let hasComputedProperties = false;
let objectFlags = ObjectFlags.ObjectLiteral;
forEach(pattern.elements, e => {
const name = e.propertyName || <Identifier>e.name;
if (isComputedNonLiteralName(name)) {
// do not include computed properties in the implied type
hasComputedProperties = true;
objectFlags |= ObjectFlags.ObjectLiteralPatternWithComputedProperties;
return;
}
if (e.dotDotDotToken) {
Expand All @@ -4331,12 +4331,11 @@ namespace ts {
members.set(symbol.escapedName, symbol);
});
const result = createAnonymousType(undefined, members, emptyArray, emptyArray, stringIndexInfo, undefined);
result.flags |= TypeFlags.ContainsObjectLiteral;
result.objectFlags |= objectFlags;
if (includePatternInType) {
result.pattern = pattern;
}
if (hasComputedProperties) {
result.objectFlags |= ObjectFlags.ObjectLiteralPatternWithComputedProperties;
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function f1([a], {b}, c, d) { // error
>d : any
}
function f2([a = undefined], {b = null}, c = undefined, d = null) { // error
>f2 : ([a]: [any], { b }: { b?: null; }, c?: any, d?: any) => void
>f2 : ([a]: [any], { b }: { b?: any; }, c?: any, d?: any) => void
>a : any
>undefined : undefined
>b : any
Expand Down
27 changes: 27 additions & 0 deletions tests/baselines/reference/parameterDestructuringObjectLiteral.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [parameterDestructuringObjectLiteral.ts]
// Repro from #22644

const fn1 = (options: { headers?: {} }) => { };
fn1({ headers: { foo: 1 } });

const fn2 = ({ headers = {} }) => { };
fn2({ headers: { foo: 1 } });


//// [parameterDestructuringObjectLiteral.js]
// Repro from #22644
var fn1 = function (options) { };
fn1({ headers: { foo: 1 } });
var fn2 = function (_a) {
var _b = _a.headers, headers = _b === void 0 ? {} : _b;
};
fn2({ headers: { foo: 1 } });


//// [parameterDestructuringObjectLiteral.d.ts]
declare const fn1: (options: {
headers?: {};
}) => void;
declare const fn2: ({ headers }: {
headers?: {};
}) => void;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/compiler/parameterDestructuringObjectLiteral.ts ===
// Repro from #22644

const fn1 = (options: { headers?: {} }) => { };
>fn1 : Symbol(fn1, Decl(parameterDestructuringObjectLiteral.ts, 2, 5))
>options : Symbol(options, Decl(parameterDestructuringObjectLiteral.ts, 2, 13))
>headers : Symbol(headers, Decl(parameterDestructuringObjectLiteral.ts, 2, 23))

fn1({ headers: { foo: 1 } });
>fn1 : Symbol(fn1, Decl(parameterDestructuringObjectLiteral.ts, 2, 5))
>headers : Symbol(headers, Decl(parameterDestructuringObjectLiteral.ts, 3, 5))
>foo : Symbol(foo, Decl(parameterDestructuringObjectLiteral.ts, 3, 16))

const fn2 = ({ headers = {} }) => { };
>fn2 : Symbol(fn2, Decl(parameterDestructuringObjectLiteral.ts, 5, 5))
>headers : Symbol(headers, Decl(parameterDestructuringObjectLiteral.ts, 5, 14))

fn2({ headers: { foo: 1 } });
>fn2 : Symbol(fn2, Decl(parameterDestructuringObjectLiteral.ts, 5, 5))
>headers : Symbol(headers, Decl(parameterDestructuringObjectLiteral.ts, 6, 5))
>foo : Symbol(foo, Decl(parameterDestructuringObjectLiteral.ts, 6, 16))

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
=== tests/cases/compiler/parameterDestructuringObjectLiteral.ts ===
// Repro from #22644

const fn1 = (options: { headers?: {} }) => { };
>fn1 : (options: { headers?: {}; }) => void
>(options: { headers?: {} }) => { } : (options: { headers?: {}; }) => void
>options : { headers?: {}; }
>headers : {}

fn1({ headers: { foo: 1 } });
>fn1({ headers: { foo: 1 } }) : void
>fn1 : (options: { headers?: {}; }) => void
>{ headers: { foo: 1 } } : { headers: { foo: number; }; }
>headers : { foo: number; }
>{ foo: 1 } : { foo: number; }
>foo : number
>1 : 1

const fn2 = ({ headers = {} }) => { };
>fn2 : ({ headers }: { headers?: {}; }) => void
>({ headers = {} }) => { } : ({ headers }: { headers?: {}; }) => void
>headers : {}
>{} : {}

fn2({ headers: { foo: 1 } });
>fn2({ headers: { foo: 1 } }) : void
>fn2 : ({ headers }: { headers?: {}; }) => void
>{ headers: { foo: 1 } } : { headers: { foo: number; }; }
>headers : { foo: number; }
>{ foo: 1 } : { foo: number; }
>foo : number
>1 : 1

9 changes: 9 additions & 0 deletions tests/cases/compiler/parameterDestructuringObjectLiteral.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @declaration: true

// Repro from #22644

const fn1 = (options: { headers?: {} }) => { };
fn1({ headers: { foo: 1 } });

const fn2 = ({ headers = {} }) => { };
fn2({ headers: { foo: 1 } });