Skip to content
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
28 changes: 28 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2292,6 +2292,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
var deferredGlobalAsyncDisposableType: ObjectType | undefined;
var deferredGlobalExtractSymbol: Symbol | undefined;
var deferredGlobalOmitSymbol: Symbol | undefined;
var deferredGlobalPartialSymbol: Symbol | undefined;
var deferredGlobalAwaitedSymbol: Symbol | undefined;
var deferredGlobalBigIntType: ObjectType | undefined;
var deferredGlobalNaNSymbol: Symbol | undefined;
Expand Down Expand Up @@ -11577,6 +11578,17 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return source;
}

// In the case where we have
// const { [key]: _, ...rest } = obj;
// where key is keyof typeof obj, then the best type for rest is Partial<typeof obj>
if (isOrContainsKeyofT(omitKeyType, source)) {
const partialTypeAlias = getGlobalPartialSymbol();
if (!partialTypeAlias) {
return errorType;
}
return getTypeAliasInstantiation(partialTypeAlias, [source]);
}

const omitTypeAlias = getGlobalOmitSymbol();
if (!omitTypeAlias) {
return errorType;
Expand All @@ -11592,6 +11604,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return result;
}

function isOrContainsKeyofT(keyType: Type, sourceType: Type): boolean {
if (keyType.flags & TypeFlags.Union) {
return !!forEach((keyType as UnionType).types, t => isOrContainsKeyofT(t, sourceType));
}
if (keyType.flags & TypeFlags.Index && (keyType as IndexType).type === sourceType) {
return true;
}
return false;
}
Comment on lines +11607 to +11615
Copy link
Member

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.


function isGenericTypeWithUndefinedConstraint(type: Type) {
return !!(type.flags & TypeFlags.Instantiable) && maybeTypeOfKind(getBaseConstraintOfType(type) || unknownType, TypeFlags.Undefined);
}
Expand Down Expand Up @@ -17573,6 +17595,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return deferredGlobalOmitSymbol === unknownSymbol ? undefined : deferredGlobalOmitSymbol;
}

function getGlobalPartialSymbol(): Symbol | undefined {
// We always report an error, so cache a result in the event we could not resolve the symbol to prevent reporting it multiple times
deferredGlobalPartialSymbol ||= getGlobalTypeAliasSymbol("Partial" as __String, /*arity*/ 1, /*reportErrors*/ true) || unknownSymbol;
return deferredGlobalPartialSymbol === unknownSymbol ? undefined : deferredGlobalPartialSymbol;
}

function getGlobalAwaitedSymbol(reportErrors: boolean): Symbol | undefined {
// Only cache `unknownSymbol` if we are reporting errors so that we don't report the error more than once.
deferredGlobalAwaitedSymbol ||= getGlobalTypeAliasSymbol("Awaited" as __String, /*arity*/ 1, reportErrors) || (reportErrors ? unknownSymbol : undefined);
Expand Down
139 changes: 139 additions & 0 deletions tests/baselines/reference/restSpreadingWithKeyofT.js
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 tests/baselines/reference/restSpreadingWithKeyofT.symbols
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))
}

Loading