-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Closed
Changes from 7 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3cf0bf7
Union literal computed properties lift to union
sandersn b39688c
Test literal union computed property lifting
sandersn d407c92
Print known constants in computed property types
sandersn 3123bef
Clean up variable naming
sandersn 6bec753
More computed property cases + Update baselines
sandersn c242f20
Improve spread type parameter names
sandersn 59095cd
Further improve readability of checkObjectLiteral
sandersn 25f981f
Print computer numeric literal property names as numeric
sandersn c69c4ff
Improve destructuring:str/num computed properties
sandersn ddd9714
Error reporting:improve destructured computed properties
sandersn ea8fc7c
Computed properties with null and undefined literals
sandersn 772c75b
Combine destructured computed property checking
sandersn 85e5054
Simplify parameter type
sandersn a979a0d
Merge branch 'master' into computed-property-union-lifting
sandersn cd3b688
Fix lint:add semicolon
sandersn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
tests/baselines/reference/computedPropertyUnionLiftsToUnionType.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
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
85
tests/baselines/reference/computedPropertyUnionLiftsToUnionType.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 eitherhas no property 'a' and no string index signature
orhas no property 'b' and no string index signature
, given the other changes you've made.There was a problem hiding this comment.
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.