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 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
327 changes: 216 additions & 111 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

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; ["true"]: number; ["undefined"]: any; ["null"]: any; } | { [x: string]: number; ["true"]: number; ["undefined"]: any; ["null"]: any; ["false"]: number; }
>{ [b]: 0, [true]: 1, [[]]: 0, [{}]: 0, [undefined]: undefined, [null]: null} : { [x: string]: number; ["true"]: number; ["undefined"]: undefined; ["null"]: null; } | { [x: string]: number; ["true"]: number; ["undefined"]: undefined; ["null"]: null; ["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; ["true"]: number; ["undefined"]: any; ["null"]: any; } | { [x: string]: number; ["true"]: number; ["undefined"]: any; ["null"]: any; ["false"]: number; }
>{ [b]: 0, [true]: 1, [[]]: 0, [{}]: 0, [undefined]: undefined, [null]: null} : { [x: string]: number; ["true"]: number; ["undefined"]: undefined; ["null"]: null; } | { [x: string]: number; ["true"]: number; ["undefined"]: undefined; ["null"]: null; ["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,76 @@
tests/cases/conformance/es6/computedProperties/computedPropertyUnionLiftsToUnionType.ts(32,4): error TS2459: Type '{ a: string; } | { b: string; }' has no property 'a' and no string index signature.
tests/cases/conformance/es6/computedProperties/computedPropertyUnionLiftsToUnionType.ts(40,7): error TS2459: Type '{ a: string; } | { b: string; }' has no property 'a' and no string index signature.
tests/cases/conformance/es6/computedProperties/computedPropertyUnionLiftsToUnionType.ts(58,11): error TS7017: Element implicitly has an 'any' type because type '{}' has no index signature.
tests/cases/conformance/es6/computedProperties/computedPropertyUnionLiftsToUnionType.ts(59,8): error TS7017: Element implicitly has an 'any' type because type '{}' has no index signature.


==== tests/cases/conformance/es6/computedProperties/computedPropertyUnionLiftsToUnionType.ts (4 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 'a' 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 'a' 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

// # 16789
declare const textMap: {[key: string]: string}

function getText (s: string, n: number) {
var { [s]: rawText = s } = sig;
var { [n]: rawNumber = n } = sin;
({ [s]: rawText } = sig);
({ [n]: rawNumber } = sin);
var { [s]: noSig } = {};
~~~
!!! error TS7017: Element implicitly has an 'any' type because type '{}' has no index signature.
({ [s]: noSig } = {});
~~~
!!! error TS7017: Element implicitly has an 'any' type because type '{}' has no index signature.
}

Loading