Skip to content

Commit 753fbac

Browse files
manudeliTkDodo
andauthored
fix(types): add number, symbol as safe key for OmitKeyof for strictness safely (#7164)
* types(query-core): add number, symbol as safe key for OmitKeyof for safely * test(query-core): add type test case for number key, symbol key --------- Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
1 parent 2c0dc08 commit 753fbac

File tree

2 files changed

+122
-2
lines changed

2 files changed

+122
-2
lines changed

packages/query-core/src/__tests__/OmitKeyof.test-d.ts

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, expectTypeOf, it } from 'vitest'
22
import type { OmitKeyof } from '..'
33

44
describe('OmitKeyof', () => {
5-
it("'s type check", () => {
5+
it("'s string key type check", () => {
66
type A = {
77
x: string
88
y: number
@@ -56,4 +56,120 @@ describe('OmitKeyof', () => {
5656
>
5757
>().toEqualTypeOf<ExpectedType>
5858
})
59+
60+
it("'s number key type check", () => {
61+
type A = {
62+
[1]: string
63+
[2]: number
64+
}
65+
66+
type ExpectedType = {
67+
[1]: string
68+
}
69+
70+
// Bad point
71+
// 1. original Omit can use 3 as type parameter with no type error
72+
// 2. original Omit have no auto complete for 2nd type parameter
73+
expectTypeOf<Omit<A, 3 | 2>>().toEqualTypeOf<ExpectedType>()
74+
75+
// Solution
76+
77+
// 1. strictly
78+
expectTypeOf<
79+
OmitKeyof<
80+
A,
81+
// OmitKeyof can't use 3 as type parameter with type error because A don't have key 3
82+
// @ts-expect-error Type does not satisfy the constraint keyof A
83+
3 | 2
84+
>
85+
>().toEqualTypeOf<ExpectedType>
86+
expectTypeOf<
87+
OmitKeyof<
88+
A,
89+
// OmitKeyof can't use 3 as type parameter with type error because A don't have key 3
90+
// @ts-expect-error Type does not satisfy the constraint keyof A
91+
3 | 2,
92+
'strictly'
93+
>
94+
>().toEqualTypeOf<ExpectedType>
95+
96+
// 2. safely
97+
expectTypeOf<
98+
OmitKeyof<
99+
A,
100+
// OmitKeyof can't use 3 as type parameter type error with strictly parameter or default parameter
101+
// @ts-expect-error Type does not satisfy the constraint keyof A
102+
3 | 2
103+
>
104+
>().toEqualTypeOf<ExpectedType>
105+
expectTypeOf<
106+
OmitKeyof<
107+
A,
108+
// With 'safely', OmitKeyof can use 3 as type parameter like original Omit but This support autocomplete too yet for DX.
109+
3 | 2,
110+
'safely'
111+
>
112+
>().toEqualTypeOf<ExpectedType>
113+
})
114+
115+
it("'s symbol key type check", () => {
116+
const symbol1 = Symbol()
117+
const symbol2 = Symbol()
118+
const symbol3 = Symbol()
119+
120+
type A = {
121+
[symbol1]: string
122+
[symbol2]: number
123+
}
124+
125+
type ExpectedType = {
126+
[symbol1]: string
127+
}
128+
129+
// Bad point
130+
// 1. original Omit can use symbol3 as type parameter with no type error
131+
// 2. original Omit have no auto complete for 2nd type parameter
132+
expectTypeOf<
133+
Omit<A, typeof symbol3 | typeof symbol2>
134+
>().toEqualTypeOf<ExpectedType>()
135+
136+
// Solution
137+
138+
// 1. strictly
139+
expectTypeOf<
140+
OmitKeyof<
141+
A,
142+
// OmitKeyof can't use symbol3 as type parameter with type error because A don't have key symbol3
143+
// @ts-expect-error Type does not satisfy the constraint keyof A
144+
typeof symbol3 | typeof symbol2
145+
>
146+
>().toEqualTypeOf<ExpectedType>
147+
expectTypeOf<
148+
OmitKeyof<
149+
A,
150+
// OmitKeyof can't use symbol3 as type parameter with type error because A don't have key symbol3
151+
// @ts-expect-error Type does not satisfy the constraint keyof A
152+
typeof symbol3 | typeof symbol2,
153+
'strictly'
154+
>
155+
>().toEqualTypeOf<ExpectedType>
156+
157+
// 2. safely
158+
expectTypeOf<
159+
OmitKeyof<
160+
A,
161+
// OmitKeyof can't use symbol3 as type parameter type error with strictly parameter or default parameter
162+
// @ts-expect-error Type does not satisfy the constraint keyof A
163+
typeof symbol3 | typeof symbol2
164+
>
165+
>().toEqualTypeOf<ExpectedType>
166+
expectTypeOf<
167+
OmitKeyof<
168+
A,
169+
// With 'safely', OmitKeyof can use symbol3 as type parameter like original Omit but This support autocomplete too yet for DX.
170+
typeof symbol3 | typeof symbol2,
171+
'safely'
172+
>
173+
>().toEqualTypeOf<ExpectedType>
174+
})
59175
})

packages/query-core/src/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import type { MutationCache } from './mutationCache'
1010
export type OmitKeyof<
1111
TObject,
1212
TKey extends TStrictly extends 'safely'
13-
? keyof TObject | (string & Record<never, never>)
13+
?
14+
| keyof TObject
15+
| (string & Record<never, never>)
16+
| (number & Record<never, never>)
17+
| (symbol & Record<never, never>)
1418
: keyof TObject,
1519
TStrictly extends 'strictly' | 'safely' = 'strictly',
1620
> = Omit<TObject, TKey>

0 commit comments

Comments
 (0)