-
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
Generic object rest variables and parameters #28312
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f555261
Add Rest<T, K> type to lib.d.ts
ahejlsberg 6f8959b
Use Rest<T, K> for rest properties in object destructuring
ahejlsberg 18f80b8
Accept new baselines
ahejlsberg b0a5337
Merge branch 'master' into genericRest
ahejlsberg 0b194a2
Define Rest<T, K> as Pick<T, Exclude<keyof T, K>> to ensure it is hom…
ahejlsberg 396642d
Only create Rest<T, K> types when object and/or key type is generic
ahejlsberg f558059
Accept new baselines
ahejlsberg 9fd7d0a
Remove Rest<T, K> and use Pick<T, Exclude<keyof T, K>> instead
ahejlsberg 9daf69d
Accept new baselines
ahejlsberg abebf15
Ensure '{ [key]: x } = obj' for generic key is the same as 'x = obj[k…
ahejlsberg bcc1d2a
Accept new baselines
ahejlsberg 48c0aed
Add tests
ahejlsberg 00c5071
Accept new baselines
ahejlsberg f9dd445
Merge branch 'master' into genericRest
ahejlsberg 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
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
19 changes: 10 additions & 9 deletions
19
tests/baselines/reference/computedPropertyNamesContextualType8_ES5.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 |
---|---|---|
@@ -1,20 +1,21 @@ | ||
tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES5.ts(7,5): error TS2418: Type of computed property's value is 'string', which is not assignable to type 'boolean'. | ||
tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES5.ts(8,5): error TS2418: Type of computed property's value is 'number', which is not assignable to type 'boolean'. | ||
tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES5.ts(6,5): error TS2322: Type '{ [x: string]: string | number; }' is not assignable to type 'I'. | ||
Index signatures are incompatible. | ||
Type 'string | number' is not assignable to type 'boolean'. | ||
Type 'string' is not assignable to type 'boolean'. | ||
|
||
|
||
==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES5.ts (2 errors) ==== | ||
==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES5.ts (1 errors) ==== | ||
interface I { | ||
[s: string]: boolean; | ||
[s: number]: boolean; | ||
} | ||
|
||
var o: I = { | ||
~ | ||
!!! error TS2322: Type '{ [x: string]: string | number; }' is not assignable to type 'I'. | ||
!!! error TS2322: Index signatures are incompatible. | ||
!!! error TS2322: Type 'string | number' is not assignable to type 'boolean'. | ||
!!! error TS2322: Type 'string' is not assignable to type 'boolean'. | ||
[""+"foo"]: "", | ||
~~~~~~~~~~ | ||
!!! error TS2418: Type of computed property's value is 'string', which is not assignable to type 'boolean'. | ||
!!! related TS6501 tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES5.ts:2:5: The expected type comes from this index signature. | ||
[""+"bar"]: 0 | ||
~~~~~~~~~~ | ||
!!! error TS2418: Type of computed property's value is 'number', which is not assignable to type 'boolean'. | ||
!!! related TS6501 tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES5.ts:2:5: The expected type comes from this index signature. | ||
} |
19 changes: 10 additions & 9 deletions
19
tests/baselines/reference/computedPropertyNamesContextualType8_ES6.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 |
---|---|---|
@@ -1,20 +1,21 @@ | ||
tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES6.ts(7,5): error TS2418: Type of computed property's value is 'string', which is not assignable to type 'boolean'. | ||
tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES6.ts(8,5): error TS2418: Type of computed property's value is 'number', which is not assignable to type 'boolean'. | ||
tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES6.ts(6,5): error TS2322: Type '{ [x: string]: string | number; }' is not assignable to type 'I'. | ||
Index signatures are incompatible. | ||
Type 'string | number' is not assignable to type 'boolean'. | ||
Type 'string' is not assignable to type 'boolean'. | ||
|
||
|
||
==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES6.ts (2 errors) ==== | ||
==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES6.ts (1 errors) ==== | ||
interface I { | ||
[s: string]: boolean; | ||
[s: number]: boolean; | ||
} | ||
|
||
var o: I = { | ||
~ | ||
!!! error TS2322: Type '{ [x: string]: string | number; }' is not assignable to type 'I'. | ||
!!! error TS2322: Index signatures are incompatible. | ||
!!! error TS2322: Type 'string | number' is not assignable to type 'boolean'. | ||
!!! error TS2322: Type 'string' is not assignable to type 'boolean'. | ||
[""+"foo"]: "", | ||
~~~~~~~~~~ | ||
!!! error TS2418: Type of computed property's value is 'string', which is not assignable to type 'boolean'. | ||
!!! related TS6501 tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES6.ts:2:5: The expected type comes from this index signature. | ||
[""+"bar"]: 0 | ||
~~~~~~~~~~ | ||
!!! error TS2418: Type of computed property's value is 'number', which is not assignable to type 'boolean'. | ||
!!! related TS6501 tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType8_ES6.ts:2:5: The expected type comes from this index signature. | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//// [genericObjectRest.ts] | ||
const a = 'a'; | ||
|
||
function f1<T extends { a: string, b: number }>(obj: T) { | ||
let { ...r0 } = obj; | ||
let { a: a1, ...r1 } = obj; | ||
let { a: a2, b: b2, ...r2 } = obj; | ||
let { 'a': a3, ...r3 } = obj; | ||
let { ['a']: a4, ...r4 } = obj; | ||
let { [a]: a5, ...r5 } = obj; | ||
} | ||
|
||
const sa = Symbol(); | ||
const sb = Symbol(); | ||
|
||
function f2<T extends { [sa]: string, [sb]: number }>(obj: T) { | ||
let { [sa]: a1, [sb]: b1, ...r1 } = obj; | ||
} | ||
|
||
function f3<T, K1 extends keyof T, K2 extends keyof T>(obj: T, k1: K1, k2: K2) { | ||
let { [k1]: a1, [k2]: a2, ...r1 } = obj; | ||
} | ||
|
||
type Item = { a: string, b: number, c: boolean }; | ||
|
||
function f4<K1 extends keyof Item, K2 extends keyof Item>(obj: Item, k1: K1, k2: K2) { | ||
let { [k1]: a1, [k2]: a2, ...r1 } = obj; | ||
} | ||
|
||
|
||
//// [genericObjectRest.js] | ||
"use strict"; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0) | ||
t[p[i]] = s[p[i]]; | ||
return t; | ||
}; | ||
const a = 'a'; | ||
function f1(obj) { | ||
let r0 = __rest(obj, []); | ||
let { a: a1 } = obj, r1 = __rest(obj, ["a"]); | ||
let { a: a2, b: b2 } = obj, r2 = __rest(obj, ["a", "b"]); | ||
let { 'a': a3 } = obj, r3 = __rest(obj, ['a']); | ||
let { ['a']: a4 } = obj, r4 = __rest(obj, ['a']); | ||
let _a = a, a5 = obj[_a], r5 = __rest(obj, [typeof _a === "symbol" ? _a : _a + ""]); | ||
} | ||
const sa = Symbol(); | ||
const sb = Symbol(); | ||
function f2(obj) { | ||
let _a = sa, a1 = obj[_a], _b = sb, b1 = obj[_b], r1 = __rest(obj, [typeof _a === "symbol" ? _a : _a + "", typeof _b === "symbol" ? _b : _b + ""]); | ||
} | ||
function f3(obj, k1, k2) { | ||
let _a = k1, a1 = obj[_a], _b = k2, a2 = obj[_b], r1 = __rest(obj, [typeof _a === "symbol" ? _a : _a + "", typeof _b === "symbol" ? _b : _b + ""]); | ||
} | ||
function f4(obj, k1, k2) { | ||
let _a = k1, a1 = obj[_a], _b = k2, a2 = obj[_b], r1 = __rest(obj, [typeof _a === "symbol" ? _a : _a + "", typeof _b === "symbol" ? _b : _b + ""]); | ||
} |
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.
Any idea what's going on here? I'm not sure why we stopped issuing the per-property error.
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 assumed it was the change in looking up the type of property names.
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.
It's because we previously treated all computed properties as having known (unit type) names, when here they actually have type
string
. Since they now have typestring
we generate a string index signature, and the elaboration logic stops trying to drill down to the properties.