Skip to content

Commit

Permalink
fix: type TData with initialData is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
liaoliao666 committed Oct 13, 2023
1 parent ba16dc2 commit 6f1dfc6
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 12 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@typescript-eslint/no-non-null-assertion": "off",
"import/no-unresolved": ["error", { "commonjs": true, "amd": true }],
"import/export": "error",
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/no-duplicate-imports": ["error"],
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-unused-vars": [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@rollup/plugin-commonjs": "^22.0.2",
"@rollup/plugin-node-resolve": "^13.2.1",
"@rollup/plugin-replace": "^4.0.0",
"@tanstack/react-query": "^5.0.0-beta.20",
"@tanstack/react-query": "^5.0.0-rc.12",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
"@trivago/prettier-plugin-sort-imports": "^4.2.0",
Expand Down
62 changes: 60 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
DefinedUseQueryResult,
GetNextPageParamFunction,
GetPreviousPageParamFunction,
InfiniteData,
Expand Down Expand Up @@ -78,6 +79,17 @@ type CompatibleUseInfiniteQueryOptions<
>
>

type CompatibleInfiniteData<TFnData, TPageParam> = CompatibleWithV4<
InfiniteData<TFnData, TPageParam>,
InfiniteData<TFnData>
>

type NonUndefinedGuard<T> = T extends undefined ? never : T

type WithRequired<T, K extends keyof T> = T & {
[_ in K]: {}
}

export type inferQueryKey<TVariables> = TVariables extends void
? [string]
: [string, TVariables]
Expand Down Expand Up @@ -168,10 +180,26 @@ export interface QueryHookOptions<TFnData, TError, TData, TVariables>
variables?: TVariables
}

export type DefinedQueryHookOptions<TFnData, TError, TData, TVariables> = Omit<
QueryHookOptions<TFnData, TError, TData, TVariables>,
'initialData'
> & {
initialData: NonUndefinedGuard<TFnData> | (() => NonUndefinedGuard<TFnData>)
}

export type QueryHookResult<TData, TError> = UseQueryResult<TData, TError>

export type DefinedQueryHookResult<TData, TError> = DefinedUseQueryResult<
TData,
TError
>

export interface QueryHook<TFnData = unknown, TVariables = any, TError = Error>
extends ExposeMethods<TFnData, TVariables> {
<TData = TFnData>(
options: DefinedQueryHookOptions<TFnData, TError, TData, TVariables>,
queryClient?: CompatibleWithV4<QueryClient, void>
): DefinedQueryHookResult<TData, TError>
<TData = TFnData>(
options?: QueryHookOptions<TFnData, TError, TData, TVariables>,
queryClient?: CompatibleWithV4<QueryClient, void>
Expand Down Expand Up @@ -279,17 +307,47 @@ export interface InfiniteQueryHookOptions<
variables?: TVariables
}

export type DefinedInfiniteQueryHookOptions<
TFnData,
TError,
TData,
TVariables,
TPageParam = number
> = Omit<
InfiniteQueryHookOptions<TFnData, TError, TData, TVariables, TPageParam>,
'initialData'
> & {
initialData:
| CompatibleInfiniteData<TFnData, TPageParam>
| (() => NonUndefinedGuard<CompatibleInfiniteData<TFnData, TPageParam>>)
}

export type InfiniteQueryHookResult<TData, TError> = UseInfiniteQueryResult<
TData,
TError
>

export type DefinedInfiniteQueryHookResult<TData, TError> = WithRequired<
UseInfiniteQueryResult<TData, TError>,
'data'
>

export interface InfiniteQueryHook<
TFnData = unknown,
TVariables = void,
TError = Error,
TPageParam = number
> extends ExposeMethods<TFnData, TVariables, TPageParam> {
<TData = CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>>(
options: DefinedInfiniteQueryHookOptions<
TFnData,
TError,
TData,
TVariables,
TPageParam
>,
queryClient?: CompatibleWithV4<QueryClient, void>
): DefinedInfiniteQueryHookResult<TData, TError>
<TData = CompatibleWithV4<InfiniteData<TFnData, TPageParam>, TFnData>>(
options?: InfiniteQueryHookOptions<
TFnData,
Expand Down Expand Up @@ -444,9 +502,9 @@ export type inferData<T> = T extends QueryHook<infer TData, any, any>
: T extends SuspenseQueryHook<infer TData, any, any>
? TData
: T extends InfiniteQueryHook<infer TData, any, any, infer TPageParam>
? CompatibleWithV4<InfiniteData<TData, TPageParam>, InfiniteData<TData>>
? CompatibleInfiniteData<TData, TPageParam>
: T extends SuspenseInfiniteQueryHook<infer TData, any, any, infer TPageParam>
? CompatibleWithV4<InfiniteData<TData, TPageParam>, InfiniteData<TData>>
? CompatibleInfiniteData<TData, TPageParam>
: T extends MutationHook<infer TData, any, any>
? TData
: never
Expand Down
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1676,17 +1676,17 @@
dependencies:
"@sinonjs/commons" "^3.0.0"

"@tanstack/[email protected]beta.20":
version "5.0.0-beta.20"
resolved "https://registry.yarnpkg.com/@tanstack/query-core/-/query-core-5.0.0-beta.20.tgz#0aad6d63ea7b407cf886117932b4ea5eb963701b"
integrity sha512-KTI6b4DLXq39n/VNLvWqxM0K/bmCyL5SvOfPGZ1cZGOvVfZxpo/81ZF9cuRDyPjInmzz8d8jUZOHZVJDKSDcvQ==
"@tanstack/[email protected]rc.12":
version "5.0.0-rc.12"
resolved "https://registry.yarnpkg.com/@tanstack/query-core/-/query-core-5.0.0-rc.12.tgz#0748043960b749888d6762bf6ed795ca7d761153"
integrity sha512-gEgRIkFMjxhu+rbomSxAYTA+aPaPrLffJwFCpfd/AqJ8PTlnredJnFfFskCbR6oZo/1XEuDORFW9c6b4gOnEKQ==

"@tanstack/react-query@^5.0.0-beta.20":
version "5.0.0-beta.20"
resolved "https://registry.yarnpkg.com/@tanstack/react-query/-/react-query-5.0.0-beta.20.tgz#d3828f9774b62f7b7e3be9da2917fe59d8488960"
integrity sha512-L5YP1zwEMBwZJ/oCnFXLnb6S60srU7Oyj/oM5TVLcC/efdKpcdToMZTHA9Vjq3oFA9ysxFgrskkideIMa3fB4A==
"@tanstack/react-query@^5.0.0-rc.12":
version "5.0.0-rc.12"
resolved "https://registry.yarnpkg.com/@tanstack/react-query/-/react-query-5.0.0-rc.12.tgz#26c61cff5c33c1be1033795de56f57eb7bb8d19a"
integrity sha512-pUb6xkHs2YFb9N6/34ZMut4LG0yO+uqjDaoWmxOsmIZVwPYCNrsRjI8kaF/6F+5gyxJp6hzFHH4Jlukbx/tReA==
dependencies:
"@tanstack/query-core" "5.0.0-beta.20"
"@tanstack/query-core" "5.0.0-rc.12"
client-only "0.0.1"

"@testing-library/dom@^9.0.0":
Expand Down

0 comments on commit 6f1dfc6

Please sign in to comment.