-
Notifications
You must be signed in to change notification settings - Fork 13k
Use Partial<T> instead of Omit<T, keyof T> when rest spreading #62573
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
52fd500
Use Partial<T> instead of Omit<T, keyof T> when rest spreading
RyanCavanaugh b1bd4cd
Initial plan
Copilot 03d7fa1
Add compiler test for Partial<T> rest spreading with keyof T
Copilot 034278d
Add contrast test for Omit cases (not using Partial)
Copilot 57242ff
Merge pull request #26 from RyanCavanaugh/copilot/add-testcases-for-p…
RyanCavanaugh 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 hidden or 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 hidden or 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,139 @@ | ||
//// [tests/cases/compiler/restSpreadingWithKeyofT.ts] //// | ||
|
||
//// [restSpreadingWithKeyofT.ts] | ||
// Test case 1: Using keyof T directly in rest spreading | ||
// Should result in Partial<T> instead of Omit<T, keyof T> | ||
function f1<T>(obj: T, key: keyof T) { | ||
const { [key]: removed, ...rest } = obj; | ||
return rest; | ||
} | ||
|
||
// Test case 2: Union of keyof T | ||
// Should result in Partial<T> since both k1 and k2 are keyof T | ||
function f2<T>(obj: T, k1: keyof T, k2: keyof T) { | ||
const { [k1]: removed1, [k2]: removed2, ...rest } = obj; | ||
return rest; | ||
} | ||
|
||
// Test case 3: keyof T with additional literal | ||
// Should still use Partial<T> since the union contains keyof T | ||
function f3<T>(obj: T, key: keyof T | "extra") { | ||
const { [key]: removed, ...rest } = obj; | ||
return rest; | ||
} | ||
|
||
// Test case 4: Specific type with keyof | ||
type MyObj = { a: string; b: number; c: boolean; }; | ||
function f4(obj: MyObj, key: keyof MyObj) { | ||
const { [key]: removed, ...rest } = obj; | ||
return rest; | ||
} | ||
|
||
// Test case 5: Constraint with keyof | ||
function f5<T extends { a: string; b: number }>(obj: T, key: keyof T) { | ||
const { [key]: removed, ...rest } = obj; | ||
return rest; | ||
} | ||
|
||
// Test case 6: Multiple parameters with keyof in object literal | ||
function f6<T>(obj: T, k1: keyof T, k2: keyof T, k3: keyof T) { | ||
const { [k1]: r1, [k2]: r2, [k3]: r3, ...rest } = obj; | ||
return rest; | ||
} | ||
|
||
// Test case 7: Nested rest with keyof | ||
function f7<T>(obj: T, key: keyof T) { | ||
const { [key]: val, ...rest } = obj; | ||
const consumed = val; | ||
return { consumed, rest }; | ||
} | ||
|
||
// Test case 8: Array of keyof (not applicable but shows edge case) | ||
function f8<T>(obj: T, keys: Array<keyof T>) { | ||
// Can't destructure with array, but showing the type relationship | ||
const key = keys[0]; | ||
const { [key]: removed, ...rest } = obj; | ||
return rest; | ||
} | ||
|
||
|
||
//// [restSpreadingWithKeyofT.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 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
// Test case 1: Using keyof T directly in rest spreading | ||
// Should result in Partial<T> instead of Omit<T, keyof T> | ||
function f1(obj, key) { | ||
var _a = obj, _b = key, removed = _a[_b], rest = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]); | ||
return rest; | ||
} | ||
// Test case 2: Union of keyof T | ||
// Should result in Partial<T> since both k1 and k2 are keyof T | ||
function f2(obj, k1, k2) { | ||
var _a = obj, _b = k1, removed1 = _a[_b], _c = k2, removed2 = _a[_c], rest = __rest(_a, [typeof _b === "symbol" ? _b : _b + "", typeof _c === "symbol" ? _c : _c + ""]); | ||
return rest; | ||
} | ||
// Test case 3: keyof T with additional literal | ||
// Should still use Partial<T> since the union contains keyof T | ||
function f3(obj, key) { | ||
var _a = obj, _b = key, removed = _a[_b], rest = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]); | ||
return rest; | ||
} | ||
function f4(obj, key) { | ||
var _a = obj, _b = key, removed = _a[_b], rest = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]); | ||
return rest; | ||
} | ||
// Test case 5: Constraint with keyof | ||
function f5(obj, key) { | ||
var _a = obj, _b = key, removed = _a[_b], rest = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]); | ||
return rest; | ||
} | ||
// Test case 6: Multiple parameters with keyof in object literal | ||
function f6(obj, k1, k2, k3) { | ||
var _a = obj, _b = k1, r1 = _a[_b], _c = k2, r2 = _a[_c], _d = k3, r3 = _a[_d], rest = __rest(_a, [typeof _b === "symbol" ? _b : _b + "", typeof _c === "symbol" ? _c : _c + "", typeof _d === "symbol" ? _d : _d + ""]); | ||
return rest; | ||
} | ||
// Test case 7: Nested rest with keyof | ||
function f7(obj, key) { | ||
var _a = obj, _b = key, val = _a[_b], rest = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]); | ||
var consumed = val; | ||
return { consumed: consumed, rest: rest }; | ||
} | ||
// Test case 8: Array of keyof (not applicable but shows edge case) | ||
function f8(obj, keys) { | ||
// Can't destructure with array, but showing the type relationship | ||
var key = keys[0]; | ||
var _a = obj, _b = key, removed = _a[_b], rest = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]); | ||
return rest; | ||
} | ||
|
||
|
||
//// [restSpreadingWithKeyofT.d.ts] | ||
declare function f1<T>(obj: T, key: keyof T): Partial<T>; | ||
declare function f2<T>(obj: T, k1: keyof T, k2: keyof T): Partial<T>; | ||
declare function f3<T>(obj: T, key: keyof T | "extra"): Partial<T>; | ||
type MyObj = { | ||
a: string; | ||
b: number; | ||
c: boolean; | ||
}; | ||
declare function f4(obj: MyObj, key: keyof MyObj): {}; | ||
declare function f5<T extends { | ||
a: string; | ||
b: number; | ||
}>(obj: T, key: keyof T): Partial<T>; | ||
declare function f6<T>(obj: T, k1: keyof T, k2: keyof T, k3: keyof T): Partial<T>; | ||
declare function f7<T>(obj: T, key: keyof T): { | ||
consumed: T[keyof T]; | ||
rest: Partial<T>; | ||
}; | ||
declare function f8<T>(obj: T, keys: Array<keyof T>): Partial<T>; |
188 changes: 188 additions & 0 deletions
188
tests/baselines/reference/restSpreadingWithKeyofT.symbols
This file contains hidden or 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,188 @@ | ||
//// [tests/cases/compiler/restSpreadingWithKeyofT.ts] //// | ||
|
||
=== restSpreadingWithKeyofT.ts === | ||
// Test case 1: Using keyof T directly in rest spreading | ||
// Should result in Partial<T> instead of Omit<T, keyof T> | ||
function f1<T>(obj: T, key: keyof T) { | ||
>f1 : Symbol(f1, Decl(restSpreadingWithKeyofT.ts, 0, 0)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 2, 12)) | ||
>obj : Symbol(obj, Decl(restSpreadingWithKeyofT.ts, 2, 15)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 2, 12)) | ||
>key : Symbol(key, Decl(restSpreadingWithKeyofT.ts, 2, 22)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 2, 12)) | ||
|
||
const { [key]: removed, ...rest } = obj; | ||
>key : Symbol(key, Decl(restSpreadingWithKeyofT.ts, 2, 22)) | ||
>removed : Symbol(removed, Decl(restSpreadingWithKeyofT.ts, 3, 11)) | ||
>rest : Symbol(rest, Decl(restSpreadingWithKeyofT.ts, 3, 27)) | ||
>obj : Symbol(obj, Decl(restSpreadingWithKeyofT.ts, 2, 15)) | ||
|
||
return rest; | ||
>rest : Symbol(rest, Decl(restSpreadingWithKeyofT.ts, 3, 27)) | ||
} | ||
|
||
// Test case 2: Union of keyof T | ||
// Should result in Partial<T> since both k1 and k2 are keyof T | ||
function f2<T>(obj: T, k1: keyof T, k2: keyof T) { | ||
>f2 : Symbol(f2, Decl(restSpreadingWithKeyofT.ts, 5, 1)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 9, 12)) | ||
>obj : Symbol(obj, Decl(restSpreadingWithKeyofT.ts, 9, 15)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 9, 12)) | ||
>k1 : Symbol(k1, Decl(restSpreadingWithKeyofT.ts, 9, 22)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 9, 12)) | ||
>k2 : Symbol(k2, Decl(restSpreadingWithKeyofT.ts, 9, 35)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 9, 12)) | ||
|
||
const { [k1]: removed1, [k2]: removed2, ...rest } = obj; | ||
>k1 : Symbol(k1, Decl(restSpreadingWithKeyofT.ts, 9, 22)) | ||
>removed1 : Symbol(removed1, Decl(restSpreadingWithKeyofT.ts, 10, 11)) | ||
>k2 : Symbol(k2, Decl(restSpreadingWithKeyofT.ts, 9, 35)) | ||
>removed2 : Symbol(removed2, Decl(restSpreadingWithKeyofT.ts, 10, 27)) | ||
>rest : Symbol(rest, Decl(restSpreadingWithKeyofT.ts, 10, 43)) | ||
>obj : Symbol(obj, Decl(restSpreadingWithKeyofT.ts, 9, 15)) | ||
|
||
return rest; | ||
>rest : Symbol(rest, Decl(restSpreadingWithKeyofT.ts, 10, 43)) | ||
} | ||
|
||
// Test case 3: keyof T with additional literal | ||
// Should still use Partial<T> since the union contains keyof T | ||
function f3<T>(obj: T, key: keyof T | "extra") { | ||
>f3 : Symbol(f3, Decl(restSpreadingWithKeyofT.ts, 12, 1)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 16, 12)) | ||
>obj : Symbol(obj, Decl(restSpreadingWithKeyofT.ts, 16, 15)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 16, 12)) | ||
>key : Symbol(key, Decl(restSpreadingWithKeyofT.ts, 16, 22)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 16, 12)) | ||
|
||
const { [key]: removed, ...rest } = obj; | ||
>key : Symbol(key, Decl(restSpreadingWithKeyofT.ts, 16, 22)) | ||
>removed : Symbol(removed, Decl(restSpreadingWithKeyofT.ts, 17, 11)) | ||
>rest : Symbol(rest, Decl(restSpreadingWithKeyofT.ts, 17, 27)) | ||
>obj : Symbol(obj, Decl(restSpreadingWithKeyofT.ts, 16, 15)) | ||
|
||
return rest; | ||
>rest : Symbol(rest, Decl(restSpreadingWithKeyofT.ts, 17, 27)) | ||
} | ||
|
||
// Test case 4: Specific type with keyof | ||
type MyObj = { a: string; b: number; c: boolean; }; | ||
>MyObj : Symbol(MyObj, Decl(restSpreadingWithKeyofT.ts, 19, 1)) | ||
>a : Symbol(a, Decl(restSpreadingWithKeyofT.ts, 22, 14)) | ||
>b : Symbol(b, Decl(restSpreadingWithKeyofT.ts, 22, 25)) | ||
>c : Symbol(c, Decl(restSpreadingWithKeyofT.ts, 22, 36)) | ||
|
||
function f4(obj: MyObj, key: keyof MyObj) { | ||
>f4 : Symbol(f4, Decl(restSpreadingWithKeyofT.ts, 22, 51)) | ||
>obj : Symbol(obj, Decl(restSpreadingWithKeyofT.ts, 23, 12)) | ||
>MyObj : Symbol(MyObj, Decl(restSpreadingWithKeyofT.ts, 19, 1)) | ||
>key : Symbol(key, Decl(restSpreadingWithKeyofT.ts, 23, 23)) | ||
>MyObj : Symbol(MyObj, Decl(restSpreadingWithKeyofT.ts, 19, 1)) | ||
|
||
const { [key]: removed, ...rest } = obj; | ||
>key : Symbol(key, Decl(restSpreadingWithKeyofT.ts, 23, 23)) | ||
>removed : Symbol(removed, Decl(restSpreadingWithKeyofT.ts, 24, 11)) | ||
>rest : Symbol(rest, Decl(restSpreadingWithKeyofT.ts, 24, 27)) | ||
>obj : Symbol(obj, Decl(restSpreadingWithKeyofT.ts, 23, 12)) | ||
|
||
return rest; | ||
>rest : Symbol(rest, Decl(restSpreadingWithKeyofT.ts, 24, 27)) | ||
} | ||
|
||
// Test case 5: Constraint with keyof | ||
function f5<T extends { a: string; b: number }>(obj: T, key: keyof T) { | ||
>f5 : Symbol(f5, Decl(restSpreadingWithKeyofT.ts, 26, 1)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 29, 12)) | ||
>a : Symbol(a, Decl(restSpreadingWithKeyofT.ts, 29, 23)) | ||
>b : Symbol(b, Decl(restSpreadingWithKeyofT.ts, 29, 34)) | ||
>obj : Symbol(obj, Decl(restSpreadingWithKeyofT.ts, 29, 48)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 29, 12)) | ||
>key : Symbol(key, Decl(restSpreadingWithKeyofT.ts, 29, 55)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 29, 12)) | ||
|
||
const { [key]: removed, ...rest } = obj; | ||
>key : Symbol(key, Decl(restSpreadingWithKeyofT.ts, 29, 55)) | ||
>removed : Symbol(removed, Decl(restSpreadingWithKeyofT.ts, 30, 11)) | ||
>rest : Symbol(rest, Decl(restSpreadingWithKeyofT.ts, 30, 27)) | ||
>obj : Symbol(obj, Decl(restSpreadingWithKeyofT.ts, 29, 48)) | ||
|
||
return rest; | ||
>rest : Symbol(rest, Decl(restSpreadingWithKeyofT.ts, 30, 27)) | ||
} | ||
|
||
// Test case 6: Multiple parameters with keyof in object literal | ||
function f6<T>(obj: T, k1: keyof T, k2: keyof T, k3: keyof T) { | ||
>f6 : Symbol(f6, Decl(restSpreadingWithKeyofT.ts, 32, 1)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 35, 12)) | ||
>obj : Symbol(obj, Decl(restSpreadingWithKeyofT.ts, 35, 15)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 35, 12)) | ||
>k1 : Symbol(k1, Decl(restSpreadingWithKeyofT.ts, 35, 22)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 35, 12)) | ||
>k2 : Symbol(k2, Decl(restSpreadingWithKeyofT.ts, 35, 35)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 35, 12)) | ||
>k3 : Symbol(k3, Decl(restSpreadingWithKeyofT.ts, 35, 48)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 35, 12)) | ||
|
||
const { [k1]: r1, [k2]: r2, [k3]: r3, ...rest } = obj; | ||
>k1 : Symbol(k1, Decl(restSpreadingWithKeyofT.ts, 35, 22)) | ||
>r1 : Symbol(r1, Decl(restSpreadingWithKeyofT.ts, 36, 11)) | ||
>k2 : Symbol(k2, Decl(restSpreadingWithKeyofT.ts, 35, 35)) | ||
>r2 : Symbol(r2, Decl(restSpreadingWithKeyofT.ts, 36, 21)) | ||
>k3 : Symbol(k3, Decl(restSpreadingWithKeyofT.ts, 35, 48)) | ||
>r3 : Symbol(r3, Decl(restSpreadingWithKeyofT.ts, 36, 31)) | ||
>rest : Symbol(rest, Decl(restSpreadingWithKeyofT.ts, 36, 41)) | ||
>obj : Symbol(obj, Decl(restSpreadingWithKeyofT.ts, 35, 15)) | ||
|
||
return rest; | ||
>rest : Symbol(rest, Decl(restSpreadingWithKeyofT.ts, 36, 41)) | ||
} | ||
|
||
// Test case 7: Nested rest with keyof | ||
function f7<T>(obj: T, key: keyof T) { | ||
>f7 : Symbol(f7, Decl(restSpreadingWithKeyofT.ts, 38, 1)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 41, 12)) | ||
>obj : Symbol(obj, Decl(restSpreadingWithKeyofT.ts, 41, 15)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 41, 12)) | ||
>key : Symbol(key, Decl(restSpreadingWithKeyofT.ts, 41, 22)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 41, 12)) | ||
|
||
const { [key]: val, ...rest } = obj; | ||
>key : Symbol(key, Decl(restSpreadingWithKeyofT.ts, 41, 22)) | ||
>val : Symbol(val, Decl(restSpreadingWithKeyofT.ts, 42, 11)) | ||
>rest : Symbol(rest, Decl(restSpreadingWithKeyofT.ts, 42, 23)) | ||
>obj : Symbol(obj, Decl(restSpreadingWithKeyofT.ts, 41, 15)) | ||
|
||
const consumed = val; | ||
>consumed : Symbol(consumed, Decl(restSpreadingWithKeyofT.ts, 43, 9)) | ||
>val : Symbol(val, Decl(restSpreadingWithKeyofT.ts, 42, 11)) | ||
|
||
return { consumed, rest }; | ||
>consumed : Symbol(consumed, Decl(restSpreadingWithKeyofT.ts, 44, 12)) | ||
>rest : Symbol(rest, Decl(restSpreadingWithKeyofT.ts, 44, 22)) | ||
} | ||
|
||
// Test case 8: Array of keyof (not applicable but shows edge case) | ||
function f8<T>(obj: T, keys: Array<keyof T>) { | ||
>f8 : Symbol(f8, Decl(restSpreadingWithKeyofT.ts, 45, 1)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 48, 12)) | ||
>obj : Symbol(obj, Decl(restSpreadingWithKeyofT.ts, 48, 15)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 48, 12)) | ||
>keys : Symbol(keys, Decl(restSpreadingWithKeyofT.ts, 48, 22)) | ||
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --)) | ||
>T : Symbol(T, Decl(restSpreadingWithKeyofT.ts, 48, 12)) | ||
|
||
// Can't destructure with array, but showing the type relationship | ||
const key = keys[0]; | ||
>key : Symbol(key, Decl(restSpreadingWithKeyofT.ts, 50, 9)) | ||
>keys : Symbol(keys, Decl(restSpreadingWithKeyofT.ts, 48, 22)) | ||
|
||
const { [key]: removed, ...rest } = obj; | ||
>key : Symbol(key, Decl(restSpreadingWithKeyofT.ts, 50, 9)) | ||
>removed : Symbol(removed, Decl(restSpreadingWithKeyofT.ts, 51, 11)) | ||
>rest : Symbol(rest, Decl(restSpreadingWithKeyofT.ts, 51, 27)) | ||
>obj : Symbol(obj, Decl(restSpreadingWithKeyofT.ts, 48, 15)) | ||
|
||
return rest; | ||
>rest : Symbol(rest, Decl(restSpreadingWithKeyofT.ts, 51, 27)) | ||
} | ||
|
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.
I think this traversal could be just a call to
someType
.