Skip to content
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

fix(query-core): add number, symbol as safe key for OmitKeyof for strictness safely #7164

Merged
merged 31 commits into from May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f6134ba
types(query-core): add number, symbol as safe key for OmitKeyof for …
manudeli Mar 23, 2024
d3ddb6c
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Mar 24, 2024
215337a
test(query-core): add type test case for number key, symbol key
manudeli Mar 24, 2024
8f846a8
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Mar 25, 2024
fe8fa66
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Mar 26, 2024
b0b3b7e
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Mar 30, 2024
3e5d5dd
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Apr 2, 2024
ee357f6
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Apr 2, 2024
d6a4251
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Apr 3, 2024
0edb4f7
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Apr 4, 2024
0ec7bfa
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Apr 4, 2024
f03b5d8
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Apr 6, 2024
17245f3
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Apr 6, 2024
ac74716
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Apr 9, 2024
04af3f7
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Apr 9, 2024
0a8756f
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Apr 9, 2024
37eae1c
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Apr 12, 2024
b9d4c59
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Apr 13, 2024
dd0c5b2
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Apr 14, 2024
39fd1c8
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Apr 16, 2024
8286d24
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Apr 21, 2024
b44c924
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Apr 25, 2024
7c73b77
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Apr 29, 2024
a084d8e
Merge branch 'main' into types/query-core/OmitKeyof
manudeli Apr 30, 2024
c0bd580
Merge branch 'main' into types/query-core/OmitKeyof
manudeli May 2, 2024
c5b4249
Merge branch 'main' into types/query-core/OmitKeyof
manudeli May 2, 2024
3b33d9d
Merge branch 'main' into types/query-core/OmitKeyof
manudeli May 3, 2024
8ae2980
Merge branch 'main' into types/query-core/OmitKeyof
manudeli May 4, 2024
2bcc20f
Merge branch 'main' into types/query-core/OmitKeyof
manudeli May 4, 2024
0e5199d
Merge branch 'main' into types/query-core/OmitKeyof
manudeli May 5, 2024
7bc46bd
Merge branch 'main' into types/query-core/OmitKeyof
TkDodo May 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
118 changes: 117 additions & 1 deletion packages/query-core/src/__tests__/OmitKeyof.test-d.ts
Expand Up @@ -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
Expand Down Expand Up @@ -56,4 +56,120 @@ describe('OmitKeyof', () => {
>
>().toEqualTypeOf<ExpectedType>
})

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<Omit<A, 3 | 2>>().toEqualTypeOf<ExpectedType>()

// 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<ExpectedType>
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<ExpectedType>

// 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<ExpectedType>
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<ExpectedType>
})

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<A, typeof symbol3 | typeof symbol2>
>().toEqualTypeOf<ExpectedType>()

// 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<ExpectedType>
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<ExpectedType>

// 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<ExpectedType>
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<ExpectedType>
})
})
6 changes: 5 additions & 1 deletion packages/query-core/src/types.ts
Expand Up @@ -10,7 +10,11 @@ import type { MutationCache } from './mutationCache'
export type OmitKeyof<
TObject,
TKey extends TStrictly extends 'safely'
? keyof TObject | (string & Record<never, never>)
?
| keyof TObject
| (string & Record<never, never>)
| (number & Record<never, never>)
| (symbol & Record<never, never>)
: keyof TObject,
TStrictly extends 'strictly' | 'safely' = 'strictly',
> = Omit<TObject, TKey>
Expand Down