Skip to content

Add support for destructuring well-known and late-bound names #23297

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 2 commits into from
Apr 11, 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: 6 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4174,14 +4174,17 @@ namespace ts {
else {
// Use explicitly specified property name ({ p: xxx } form), or otherwise the implied name ({ p } form)
const name = declaration.propertyName || <Identifier>declaration.name;
if (isComputedNonLiteralName(name)) {
// computed properties with non-literal names are treated as 'any'
const isLate = isLateBindableName(name);
const isWellKnown = isComputedPropertyName(name) && isWellKnownSymbolSyntactically(name.expression);
if (!isLate && !isWellKnown && isComputedNonLiteralName(name)) {
return anyType;
}

// Use type of the specified property, or otherwise, for a numeric name, the type of the numeric index signature,
// or otherwise the type of the string index signature.
const text = getTextOfPropertyName(name);
const text = isLate ? getLateBoundNameFromType(checkComputedPropertyName(name as ComputedPropertyName) as LiteralType | UniqueESSymbolType) :
isWellKnown ? getPropertyNameForKnownSymbolName(idText(((name as ComputedPropertyName).expression as PropertyAccessExpression).name)) :
getTextOfPropertyName(name);

// Relax null check on ambient destructuring parameters, since the parameters have no implementation and are just documentation
if (strictNullChecks && declaration.flags & NodeFlags.Ambient && isParameterDeclaration(declaration)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
tests/cases/compiler/destructuredLateBoundNameHasCorrectTypes.ts(11,7): error TS2459: Type '{ prop: string; }' has no property '[notPresent]' and no string index signature.


==== tests/cases/compiler/destructuredLateBoundNameHasCorrectTypes.ts (1 errors) ====
let { [Symbol.iterator]: destructured } = [];
void destructured;

const named = "prop";

let { [named]: computed } = { prop: "b" };
void computed;

const notPresent = "prop2";

let { [notPresent]: computed2 } = { prop: "b" };
~~~~~~~~~~~~
!!! error TS2459: Type '{ prop: string; }' has no property '[notPresent]' and no string index signature.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//// [destructuredLateBoundNameHasCorrectTypes.ts]
let { [Symbol.iterator]: destructured } = [];
void destructured;

const named = "prop";

let { [named]: computed } = { prop: "b" };
void computed;

const notPresent = "prop2";

let { [notPresent]: computed2 } = { prop: "b" };


//// [destructuredLateBoundNameHasCorrectTypes.js]
let { [Symbol.iterator]: destructured } = [];
void destructured;
const named = "prop";
let { [named]: computed } = { prop: "b" };
void computed;
const notPresent = "prop2";
let { [notPresent]: computed2 } = { prop: "b" };
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/compiler/destructuredLateBoundNameHasCorrectTypes.ts ===
let { [Symbol.iterator]: destructured } = [];
>Symbol.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
>Symbol : Symbol(Symbol, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --))
>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
>destructured : Symbol(destructured, Decl(destructuredLateBoundNameHasCorrectTypes.ts, 0, 5))

void destructured;
>destructured : Symbol(destructured, Decl(destructuredLateBoundNameHasCorrectTypes.ts, 0, 5))

const named = "prop";
>named : Symbol(named, Decl(destructuredLateBoundNameHasCorrectTypes.ts, 3, 5))

let { [named]: computed } = { prop: "b" };
>named : Symbol(named, Decl(destructuredLateBoundNameHasCorrectTypes.ts, 3, 5))
>computed : Symbol(computed, Decl(destructuredLateBoundNameHasCorrectTypes.ts, 5, 5))
>prop : Symbol(prop, Decl(destructuredLateBoundNameHasCorrectTypes.ts, 5, 29))

void computed;
>computed : Symbol(computed, Decl(destructuredLateBoundNameHasCorrectTypes.ts, 5, 5))

const notPresent = "prop2";
>notPresent : Symbol(notPresent, Decl(destructuredLateBoundNameHasCorrectTypes.ts, 8, 5))

let { [notPresent]: computed2 } = { prop: "b" };
>notPresent : Symbol(notPresent, Decl(destructuredLateBoundNameHasCorrectTypes.ts, 8, 5))
>computed2 : Symbol(computed2, Decl(destructuredLateBoundNameHasCorrectTypes.ts, 10, 5))
>prop : Symbol(prop, Decl(destructuredLateBoundNameHasCorrectTypes.ts, 10, 35))

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
=== tests/cases/compiler/destructuredLateBoundNameHasCorrectTypes.ts ===
let { [Symbol.iterator]: destructured } = [];
>Symbol.iterator : symbol
>Symbol : SymbolConstructor
>iterator : symbol
>destructured : () => IterableIterator<undefined>
>[] : undefined[]

void destructured;
>void destructured : undefined
>destructured : () => IterableIterator<undefined>

const named = "prop";
>named : "prop"
>"prop" : "prop"

let { [named]: computed } = { prop: "b" };
>named : "prop"
>computed : string
>{ prop: "b" } : { prop: string; }
>prop : string
>"b" : "b"

void computed;
>void computed : undefined
>computed : string

const notPresent = "prop2";
>notPresent : "prop2"
>"prop2" : "prop2"

let { [notPresent]: computed2 } = { prop: "b" };
>notPresent : "prop2"
>computed2 : any
>{ prop: "b" } : { prop: string; }
>prop : string
>"b" : "b"

12 changes: 12 additions & 0 deletions tests/cases/compiler/destructuredLateBoundNameHasCorrectTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @target: es6
let { [Symbol.iterator]: destructured } = [];
void destructured;

const named = "prop";

let { [named]: computed } = { prop: "b" };
void computed;

const notPresent = "prop2";

let { [notPresent]: computed2 } = { prop: "b" };