-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
types: add
OmitKeyof
to Omit object by object's keys strictly (#7139)
* feat(*): add OmitKeyOf to validate Omitting keys * Update packages/vue-query/src/__tests__/useQueries.types.test.ts * Update packages/vue-query/src/__tests__/useQueries.types.test.ts * chore(*): update OmitKeyof's type Co-authored-by: 2-NOW <[email protected]> * test(query-core): add test case for OmitKeyof * chore: update --------- Co-authored-by: 2-NOW <[email protected]>
- Loading branch information
Showing
32 changed files
with
180 additions
and
69 deletions.
There are no files selected for viewing
This file contains 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 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 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,59 @@ | ||
import { describe, expectTypeOf, it } from 'vitest' | ||
import type { OmitKeyof } from '..' | ||
|
||
describe('OmitKeyof', () => { | ||
it("'s type check", () => { | ||
type A = { | ||
x: string | ||
y: number | ||
} | ||
|
||
type ExpectedType = { | ||
x: string | ||
} | ||
|
||
// Bad point | ||
// 1. original Omit can use 'z' as type parameter with no type error | ||
// 2. original Omit have no auto complete for 2nd type parameter | ||
expectTypeOf<Omit<A, 'z' | 'y'>>().toEqualTypeOf<ExpectedType>() | ||
|
||
// Solution | ||
|
||
// 1. strictly | ||
expectTypeOf< | ||
OmitKeyof< | ||
A, | ||
// OmitKeyof can't use 'z' as type parameter with type error because A don't have key 'z' | ||
// @ts-expect-error Type does not satisfy the constraint keyof A | ||
'z' | 'y' | ||
> | ||
>().toEqualTypeOf<ExpectedType> | ||
expectTypeOf< | ||
OmitKeyof< | ||
A, | ||
// OmitKeyof can't use 'z' as type parameter with type error because A don't have key 'z' | ||
// @ts-expect-error Type does not satisfy the constraint keyof A | ||
'z' | 'y', | ||
'strictly' | ||
> | ||
>().toEqualTypeOf<ExpectedType> | ||
|
||
// 2. safely | ||
expectTypeOf< | ||
OmitKeyof< | ||
A, | ||
// OmitKeyof can't use 'z' as type parameter type error with strictly parameter or default parameter | ||
// @ts-expect-error Type does not satisfy the constraint keyof A | ||
'z' | 'y' | ||
> | ||
>().toEqualTypeOf<ExpectedType> | ||
expectTypeOf< | ||
OmitKeyof< | ||
A, | ||
// With 'safely', OmitKeyof can use 'z' as type parameter like original Omit but This support autocomplete too yet for DX. | ||
'z' | 'y', | ||
'safely' | ||
> | ||
>().toEqualTypeOf<ExpectedType> | ||
}) | ||
}) |
This file contains 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 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 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 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 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 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 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 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 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
Oops, something went wrong.