Skip to content
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

Computed property union lifting #21070

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 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
316 changes: 207 additions & 109 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames10_ES5.types
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ var a: any;
>a : any

var v = {
>v : { [x: string]: () => void; [x: number]: () => void; [""](): void; [0](): void; }
>{ [s]() { }, [n]() { }, [s + s]() { }, [s + n]() { }, [+s]() { }, [""]() { }, [0]() { }, [a]() { }, [<any>true]() { }, [`hello bye`]() { }, [`hello ${a} bye`]() { }} : { [x: string]: () => void; [x: number]: () => void; [""](): void; [0](): void; }
>v : { [x: string]: () => void; [x: number]: () => void; [""](): void; ["0"](): void; }
>{ [s]() { }, [n]() { }, [s + s]() { }, [s + n]() { }, [+s]() { }, [""]() { }, [0]() { }, [a]() { }, [<any>true]() { }, [`hello bye`]() { }, [`hello ${a} bye`]() { }} : { [x: string]: () => void; [x: number]: () => void; [""](): void; ["0"](): void; }

[s]() { },
>s : string
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames10_ES6.types
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ var a: any;
>a : any

var v = {
>v : { [x: string]: () => void; [x: number]: () => void; [""](): void; [0](): void; }
>{ [s]() { }, [n]() { }, [s + s]() { }, [s + n]() { }, [+s]() { }, [""]() { }, [0]() { }, [a]() { }, [<any>true]() { }, [`hello bye`]() { }, [`hello ${a} bye`]() { }} : { [x: string]: () => void; [x: number]: () => void; [""](): void; [0](): void; }
>v : { [x: string]: () => void; [x: number]: () => void; [""](): void; ["0"](): void; }
>{ [s]() { }, [n]() { }, [s + s]() { }, [s + n]() { }, [+s]() { }, [""]() { }, [0]() { }, [a]() { }, [<any>true]() { }, [`hello bye`]() { }, [`hello ${a} bye`]() { }} : { [x: string]: () => void; [x: number]: () => void; [""](): void; ["0"](): void; }

[s]() { },
>s : string
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames46_ES5.types
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
=== tests/cases/conformance/es6/computedProperties/computedPropertyNames46_ES5.ts ===
var o = {
>o : { ["" || 0]: number; }
>{ ["" || 0]: 0} : { ["" || 0]: number; }
>o : { ["0"]: number; }
>{ ["" || 0]: 0} : { ["0"]: number; }

["" || 0]: 0
>"" || 0 : 0
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames46_ES6.types
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
=== tests/cases/conformance/es6/computedProperties/computedPropertyNames46_ES6.ts ===
var o = {
>o : { ["" || 0]: number; }
>{ ["" || 0]: 0} : { ["" || 0]: number; }
>o : { ["0"]: number; }
>{ ["" || 0]: 0} : { ["0"]: number; }

["" || 0]: 0
>"" || 0 : 0
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames47_ES5.types
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ enum E2 { x }
>x : E2

var o = {
>o : { [E1.x || E2.x]: number; }
>{ [E1.x || E2.x]: 0} : { [E1.x || E2.x]: number; }
>o : { ["0"]: number; }
>{ [E1.x || E2.x]: 0} : { ["0"]: number; }

[E1.x || E2.x]: 0
>E1.x || E2.x : E2
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames47_ES6.types
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ enum E2 { x }
>x : E2

var o = {
>o : { [E1.x || E2.x]: number; }
>{ [E1.x || E2.x]: 0} : { [E1.x || E2.x]: number; }
>o : { ["0"]: number; }
>{ [E1.x || E2.x]: 0} : { ["0"]: number; }

[E1.x || E2.x]: 0
>E1.x || E2.x : E2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extractIndexer({
extractIndexer({
>extractIndexer({ ["" || 0]: ""}) : string
>extractIndexer : <T>(p: { [n: number]: T; }) => T
>{ ["" || 0]: ""} : { ["" || 0]: string; }
>{ ["" || 0]: ""} : { ["0"]: string; }

["" || 0]: ""
>"" || 0 : 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extractIndexer({
extractIndexer({
>extractIndexer({ ["" || 0]: ""}) : string
>extractIndexer : <T>(p: { [n: number]: T; }) => T
>{ ["" || 0]: ""} : { ["" || 0]: string; }
>{ ["" || 0]: ""} : { ["0"]: string; }

["" || 0]: ""
>"" || 0 : 0
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames4_ES5.types
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ var a: any;
>a : any

var v = {
>v : { [x: string]: string | number; [x: number]: string | number; [""]: number; [0]: number; [`hello bye`]: number; }
>{ [s]: 0, [n]: n, [s + s]: 1, [s + n]: 2, [+s]: s, [""]: 0, [0]: 0, [a]: 1, [<any>true]: 0, [`hello bye`]: 0, [`hello ${a} bye`]: 0} : { [x: string]: string | number; [x: number]: string | number; [""]: number; [0]: number; [`hello bye`]: number; }
>v : { [x: string]: string | number; [x: number]: string | number; [""]: number; ["0"]: number; ["hello bye"]: number; }
>{ [s]: 0, [n]: n, [s + s]: 1, [s + n]: 2, [+s]: s, [""]: 0, [0]: 0, [a]: 1, [<any>true]: 0, [`hello bye`]: 0, [`hello ${a} bye`]: 0} : { [x: string]: string | number; [x: number]: string | number; [""]: number; ["0"]: number; ["hello bye"]: number; }

[s]: 0,
>s : string
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames4_ES6.types
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ var a: any;
>a : any

var v = {
>v : { [x: string]: string | number; [x: number]: string | number; [""]: number; [0]: number; [`hello bye`]: number; }
>{ [s]: 0, [n]: n, [s + s]: 1, [s + n]: 2, [+s]: s, [""]: 0, [0]: 0, [a]: 1, [<any>true]: 0, [`hello bye`]: 0, [`hello ${a} bye`]: 0} : { [x: string]: string | number; [x: number]: string | number; [""]: number; [0]: number; [`hello bye`]: number; }
>v : { [x: string]: string | number; [x: number]: string | number; [""]: number; ["0"]: number; ["hello bye"]: number; }
>{ [s]: 0, [n]: n, [s + s]: 1, [s + n]: 2, [+s]: s, [""]: 0, [0]: 0, [a]: 1, [<any>true]: 0, [`hello bye`]: 0, [`hello ${a} bye`]: 0} : { [x: string]: string | number; [x: number]: string | number; [""]: number; ["0"]: number; ["hello bye"]: number; }

[s]: 0,
>s : string
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames5_ES5.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ var b: boolean;
>b : boolean

var v = {
>v : { [x: string]: number; [x: number]: any; [true]: number; }
>{ [b]: 0, [true]: 1, [[]]: 0, [{}]: 0, [undefined]: undefined, [null]: null} : { [x: string]: number; [x: number]: null; [true]: number; }
>v : { [x: string]: number; [x: number]: any; ["true"]: number; } | { [x: string]: number; [x: number]: any; ["true"]: number; ["false"]: number; }
>{ [b]: 0, [true]: 1, [[]]: 0, [{}]: 0, [undefined]: undefined, [null]: null} : { [x: string]: number; [x: number]: null; ["true"]: number; } | { [x: string]: number; [x: number]: null; ["true"]: number; ["false"]: number; }

[b]: 0,
>b : boolean
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames5_ES6.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ var b: boolean;
>b : boolean

var v = {
>v : { [x: string]: number; [x: number]: any; [true]: number; }
>{ [b]: 0, [true]: 1, [[]]: 0, [{}]: 0, [undefined]: undefined, [null]: null} : { [x: string]: number; [x: number]: null; [true]: number; }
>v : { [x: string]: number; [x: number]: any; ["true"]: number; } | { [x: string]: number; [x: number]: any; ["true"]: number; ["false"]: number; }
>{ [b]: 0, [true]: 1, [[]]: 0, [{}]: 0, [undefined]: undefined, [null]: null} : { [x: string]: number; [x: number]: null; ["true"]: number; } | { [x: string]: number; [x: number]: null; ["true"]: number; ["false"]: number; }

[b]: 0,
>b : boolean
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames9_ES5.types
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ function f(x): any { }
>x : any

var v = {
>v : { [x: string]: number; [x: number]: number; [f(true)]: number; }
>{ [f("")]: 0, [f(0)]: 0, [f(true)]: 0} : { [x: string]: number; [x: number]: number; [f(true)]: number; }
>v : { [x: string]: number; [x: number]: number; ["true"]: number; }
>{ [f("")]: 0, [f(0)]: 0, [f(true)]: 0} : { [x: string]: number; [x: number]: number; ["true"]: number; }

[f("")]: 0,
>f("") : string
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/computedPropertyNames9_ES6.types
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ function f(x): any { }
>x : any

var v = {
>v : { [x: string]: number; [x: number]: number; [f(true)]: number; }
>{ [f("")]: 0, [f(0)]: 0, [f(true)]: 0} : { [x: string]: number; [x: number]: number; [f(true)]: number; }
>v : { [x: string]: number; [x: number]: number; ["true"]: number; }
>{ [f("")]: 0, [f(0)]: 0, [f(true)]: 0} : { [x: string]: number; [x: number]: number; ["true"]: number; }

[f("")]: 0,
>f("") : string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
tests/cases/conformance/es6/computedProperties/computedPropertyUnionLiftsToUnionType.ts(32,4): error TS2459: Type '{ a: string; } | { b: string; }' has no property '[ab]' and no string index signature.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has no property '[ab]' and no string index signature - this error seems somewhat lacking. I'd expect either has no property 'a' and no string index signature or has no property 'b' and no string index signature, given the other changes you've made.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I improved the error reporting and put it behind the noImplicitAny flag where it belongs.

tests/cases/conformance/es6/computedProperties/computedPropertyUnionLiftsToUnionType.ts(40,7): error TS2459: Type '{ a: string; } | { b: string; }' has no property '[ab]' and no string index signature.


==== tests/cases/conformance/es6/computedProperties/computedPropertyUnionLiftsToUnionType.ts (2 errors) ====
declare var ab: 'a' | 'b';
declare var cd: 'c' | 'd';
declare var onetwo: 1 | 2;
enum Alphabet {
Aleph,
Bet,
}
declare var alphabet: Alphabet;

const x: { a: string } | { b: string } = { [ab]: 'hi' }
// multiple unions
const y: { a: string, m: number, c: string }
| { a: string, m: number, d: string }
| { b: string, m: number, c: string }
| { b: string, m: number, d: string } = { [ab]: 'hi', m: 1, [cd]: 'there' }
// union, spread (with union inside), union
const s: { a: string, c: string } | { b: string, c: string } = { [ab]: 'hi', ...{ c: 'no' }}
const sd: { a: string } | { b: string } = { [ab]: 'hi', ...{ a: 'no' }}
const sn: { a: string, c: string }
| { a: string, d: string }
| { b: string, c: string }
| { b: string, d: string } = { [ab]: 'hi', ...{ [cd]: 'no' }}
// methods
const m: { a: string, m(): number, p: number } | { b: string, m(): number, p: number } =
{ [ab]: 'hi', m() { return 1 }, get p() { return 2 } }
// other literal types: number, enum (string and number)
const n: { "1": string } | { "2": string } = { [onetwo]: 'hi' }
const e: { "0": string } | { "1": string } = { [alphabet]: 'hi' }

// destructuring
declare let u: { a: string } | { b: string }
({ [ab]: du } = u) // implicit any error
~~~~
!!! error TS2459: Type '{ a: string; } | { b: string; }' has no property '[ab]' and no string index signature.
var du: any
declare let sig: { [s: string]: string }
({ [ab]: ds } = sig) // fine, comes from index signature
var ds: string

var duo: any
var dso: string
var { [ab]: duo } = u // implicit any error (or similar to the singleton one)
~~~~
!!! error TS2459: Type '{ a: string; } | { b: string; }' has no property '[ab]' and no string index signature.
var { [ab]: dso } = sig // fine

// number index signatures
declare let sin: { [n: number]: number }
var dn: number
({ [onetwo]: dn } = sin) // fine, from index signature
var dno: number
var { [onetwo]: dno } = sin // fine, from index signature





85 changes: 85 additions & 0 deletions tests/baselines/reference/computedPropertyUnionLiftsToUnionType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//// [computedPropertyUnionLiftsToUnionType.ts]
declare var ab: 'a' | 'b';
declare var cd: 'c' | 'd';
declare var onetwo: 1 | 2;
enum Alphabet {
Aleph,
Bet,
}
declare var alphabet: Alphabet;

const x: { a: string } | { b: string } = { [ab]: 'hi' }
// multiple unions
const y: { a: string, m: number, c: string }
| { a: string, m: number, d: string }
| { b: string, m: number, c: string }
| { b: string, m: number, d: string } = { [ab]: 'hi', m: 1, [cd]: 'there' }
// union, spread (with union inside), union
const s: { a: string, c: string } | { b: string, c: string } = { [ab]: 'hi', ...{ c: 'no' }}
const sd: { a: string } | { b: string } = { [ab]: 'hi', ...{ a: 'no' }}
const sn: { a: string, c: string }
| { a: string, d: string }
| { b: string, c: string }
| { b: string, d: string } = { [ab]: 'hi', ...{ [cd]: 'no' }}
// methods
const m: { a: string, m(): number, p: number } | { b: string, m(): number, p: number } =
{ [ab]: 'hi', m() { return 1 }, get p() { return 2 } }
// other literal types: number, enum (string and number)
const n: { "1": string } | { "2": string } = { [onetwo]: 'hi' }
const e: { "0": string } | { "1": string } = { [alphabet]: 'hi' }

// destructuring
declare let u: { a: string } | { b: string }
({ [ab]: du } = u) // implicit any error
var du: any
declare let sig: { [s: string]: string }
({ [ab]: ds } = sig) // fine, comes from index signature
var ds: string

var duo: any
var dso: string
var { [ab]: duo } = u // implicit any error (or similar to the singleton one)
var { [ab]: dso } = sig // fine

// number index signatures
declare let sin: { [n: number]: number }
var dn: number
({ [onetwo]: dn } = sin) // fine, from index signature
var dno: number
var { [onetwo]: dno } = sin // fine, from index signature






//// [computedPropertyUnionLiftsToUnionType.js]
var Alphabet;
(function (Alphabet) {
Alphabet[Alphabet["Aleph"] = 0] = "Aleph";
Alphabet[Alphabet["Bet"] = 1] = "Bet";
})(Alphabet || (Alphabet = {}));
const x = { [ab]: 'hi' };
// multiple unions
const y = { [ab]: 'hi', m: 1, [cd]: 'there' };
// union, spread (with union inside), union
const s = Object.assign({ [ab]: 'hi' }, { c: 'no' });
const sd = Object.assign({ [ab]: 'hi' }, { a: 'no' });
const sn = Object.assign({ [ab]: 'hi' }, { [cd]: 'no' });
// methods
const m = { [ab]: 'hi', m() { return 1; }, get p() { return 2; } };
// other literal types: number, enum (string and number)
const n = { [onetwo]: 'hi' };
const e = { [alphabet]: 'hi' };
({ [ab]: du } = u); // implicit any error
var du;
({ [ab]: ds } = sig); // fine, comes from index signature
var ds;
var duo;
var dso;
var { [ab]: duo } = u; // implicit any error (or similar to the singleton one)
var { [ab]: dso } = sig; // fine
var dn;
({ [onetwo]: dn } = sin); // fine, from index signature
var dno;
var { [onetwo]: dno } = sin; // fine, from index signature
Loading