From f6134ba84bbab27b67c56cbcc25b6a0948ae01ae Mon Sep 17 00:00:00 2001 From: Jonghyeon Ko Date: Sat, 23 Mar 2024 12:22:36 +0900 Subject: [PATCH 1/2] types(query-core): add number, symbol as safe key for OmitKeyof for safely --- packages/query-core/src/types.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/query-core/src/types.ts b/packages/query-core/src/types.ts index b7bcc1ffbc..c6ac7dc2e1 100644 --- a/packages/query-core/src/types.ts +++ b/packages/query-core/src/types.ts @@ -10,7 +10,11 @@ import type { MutationCache } from './mutationCache' export type OmitKeyof< TObject, TKey extends TStrictly extends 'safely' - ? keyof TObject | (string & Record) + ? + | keyof TObject + | (string & Record) + | (number & Record) + | (symbol & Record) : keyof TObject, TStrictly extends 'strictly' | 'safely' = 'strictly', > = Omit From 215337a0282f20191b9b610bdb47ae903de26cc6 Mon Sep 17 00:00:00 2001 From: Jonghyeon Ko Date: Sun, 24 Mar 2024 22:46:02 +0900 Subject: [PATCH 2/2] test(query-core): add type test case for number key, symbol key --- .../src/__tests__/OmitKeyof.test-d.ts | 118 +++++++++++++++++- 1 file changed, 117 insertions(+), 1 deletion(-) diff --git a/packages/query-core/src/__tests__/OmitKeyof.test-d.ts b/packages/query-core/src/__tests__/OmitKeyof.test-d.ts index caef030070..21898cd311 100644 --- a/packages/query-core/src/__tests__/OmitKeyof.test-d.ts +++ b/packages/query-core/src/__tests__/OmitKeyof.test-d.ts @@ -2,7 +2,7 @@ import { describe, expectTypeOf, it } from 'vitest' import type { OmitKeyof } from '..' describe('OmitKeyof', () => { - it("'s type check", () => { + it("'s string key type check", () => { type A = { x: string y: number @@ -56,4 +56,120 @@ describe('OmitKeyof', () => { > >().toEqualTypeOf }) + + it("'s number key type check", () => { + type A = { + [1]: string + [2]: number + } + + type ExpectedType = { + [1]: string + } + + // Bad point + // 1. original Omit can use 3 as type parameter with no type error + // 2. original Omit have no auto complete for 2nd type parameter + expectTypeOf>().toEqualTypeOf() + + // Solution + + // 1. strictly + expectTypeOf< + OmitKeyof< + A, + // OmitKeyof can't use 3 as type parameter with type error because A don't have key 3 + // @ts-expect-error Type does not satisfy the constraint keyof A + 3 | 2 + > + >().toEqualTypeOf + expectTypeOf< + OmitKeyof< + A, + // OmitKeyof can't use 3 as type parameter with type error because A don't have key 3 + // @ts-expect-error Type does not satisfy the constraint keyof A + 3 | 2, + 'strictly' + > + >().toEqualTypeOf + + // 2. safely + expectTypeOf< + OmitKeyof< + A, + // OmitKeyof can't use 3 as type parameter type error with strictly parameter or default parameter + // @ts-expect-error Type does not satisfy the constraint keyof A + 3 | 2 + > + >().toEqualTypeOf + expectTypeOf< + OmitKeyof< + A, + // With 'safely', OmitKeyof can use 3 as type parameter like original Omit but This support autocomplete too yet for DX. + 3 | 2, + 'safely' + > + >().toEqualTypeOf + }) + + it("'s symbol key type check", () => { + const symbol1 = Symbol() + const symbol2 = Symbol() + const symbol3 = Symbol() + + type A = { + [symbol1]: string + [symbol2]: number + } + + type ExpectedType = { + [symbol1]: string + } + + // Bad point + // 1. original Omit can use symbol3 as type parameter with no type error + // 2. original Omit have no auto complete for 2nd type parameter + expectTypeOf< + Omit + >().toEqualTypeOf() + + // Solution + + // 1. strictly + expectTypeOf< + OmitKeyof< + A, + // OmitKeyof can't use symbol3 as type parameter with type error because A don't have key symbol3 + // @ts-expect-error Type does not satisfy the constraint keyof A + typeof symbol3 | typeof symbol2 + > + >().toEqualTypeOf + expectTypeOf< + OmitKeyof< + A, + // OmitKeyof can't use symbol3 as type parameter with type error because A don't have key symbol3 + // @ts-expect-error Type does not satisfy the constraint keyof A + typeof symbol3 | typeof symbol2, + 'strictly' + > + >().toEqualTypeOf + + // 2. safely + expectTypeOf< + OmitKeyof< + A, + // OmitKeyof can't use symbol3 as type parameter type error with strictly parameter or default parameter + // @ts-expect-error Type does not satisfy the constraint keyof A + typeof symbol3 | typeof symbol2 + > + >().toEqualTypeOf + expectTypeOf< + OmitKeyof< + A, + // With 'safely', OmitKeyof can use symbol3 as type parameter like original Omit but This support autocomplete too yet for DX. + typeof symbol3 | typeof symbol2, + 'safely' + > + >().toEqualTypeOf + }) })