Skip to content

Commit

Permalink
feat(types): type narrowing for isPlaceholderData (#8586)
Browse files Browse the repository at this point in the history
* feat(types): type narrowing for isPlaceholderData

* fix: infiniteQueryObserver types

* fix: typing issues 🤷‍♂️

* chore: add missing tsconfig.legacy.json files

* fix: run type-tests in serial
  • Loading branch information
TkDodo authored Jan 27, 2025
1 parent 97f5544 commit 2399bb0
Show file tree
Hide file tree
Showing 24 changed files with 319 additions and 50 deletions.
3 changes: 2 additions & 1 deletion packages/angular-query-devtools-experimental/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"clean": "premove ./build ./coverage ./dist-ts",
"compile": "tsc --build",
"test:eslint": "eslint ./src",
"test:types": "pnpm run \"/^test:types:ts[0-9]{2}$/\"",
"test:types": "npm-run-all --serial test:types:*",
"test:types:ts51": "node ../../node_modules/typescript51/lib/tsc.js --build",
"test:types:ts52": "node ../../node_modules/typescript52/lib/tsc.js --build",
"test:types:ts53": "node ../../node_modules/typescript53/lib/tsc.js --build",
Expand Down Expand Up @@ -57,6 +57,7 @@
"@angular/platform-browser-dynamic": "^19.1.0-next.0",
"@tanstack/angular-query-experimental": "workspace:*",
"eslint-plugin-jsdoc": "^50.5.0",
"npm-run-all": "^4.1.5",
"tsup": "8.0.2",
"typescript": "5.7.2"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/angular-query-experimental/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"clean": "premove ./build ./coverage ./dist-ts",
"compile": "tsc --build",
"test:eslint": "eslint ./src",
"test:types": "pnpm run \"/^test:types:ts[0-9]{2}$/\"",
"test:types": "npm-run-all --serial test:types:*",
"test:types:ts50": "node ../../node_modules/typescript50/lib/tsc.js --build",
"test:types:ts51": "node ../../node_modules/typescript51/lib/tsc.js --build",
"test:types:ts52": "node ../../node_modules/typescript52/lib/tsc.js --build",
Expand Down Expand Up @@ -75,6 +75,7 @@
"@angular/platform-browser-dynamic": "^19.1.0-next.0",
"@microsoft/api-extractor": "^7.48.1",
"eslint-plugin-jsdoc": "^50.5.0",
"npm-run-all": "^4.1.5",
"tsup": "8.0.2",
"typescript": "5.7.2"
},
Expand Down
5 changes: 3 additions & 2 deletions packages/eslint-plugin-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"clean": "premove ./dist ./coverage ./dist-ts",
"compile": "tsc --build",
"test:eslint": "eslint ./src",
"test:types": "pnpm run \"/^test:types:ts[0-9]{2}$/\"",
"test:types": "npm-run-all --serial test:types:*",
"test:types:ts50": "node ../../node_modules/typescript50/lib/tsc.js --build",
"test:types:ts51": "node ../../node_modules/typescript51/lib/tsc.js --build",
"test:types:ts52": "node ../../node_modules/typescript52/lib/tsc.js --build",
Expand Down Expand Up @@ -61,7 +61,8 @@
"devDependencies": {
"@typescript-eslint/rule-tester": "^8.18.1",
"combinate": "^1.1.11",
"eslint": "^9.15.0"
"eslint": "^9.15.0",
"npm-run-all": "^4.1.5"
},
"peerDependencies": {
"eslint": "^8.57.0 || ^9.0.0"
Expand Down
5 changes: 4 additions & 1 deletion packages/query-async-storage-persister/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"clean": "premove ./build ./coverage ./dist-ts",
"compile": "tsc --build",
"test:eslint": "eslint ./src",
"test:types": "pnpm run \"/^test:types:ts[0-9]{2}$/\"",
"test:types": "npm-run-all --serial test:types:*",
"test:types:ts50": "node ../../node_modules/typescript50/lib/tsc.js --build",
"test:types:ts51": "node ../../node_modules/typescript51/lib/tsc.js --build",
"test:types:ts52": "node ../../node_modules/typescript52/lib/tsc.js --build",
Expand Down Expand Up @@ -57,5 +57,8 @@
],
"dependencies": {
"@tanstack/query-persist-client-core": "workspace:*"
},
"devDependencies": {
"npm-run-all": "^4.1.5"
}
}
5 changes: 4 additions & 1 deletion packages/query-broadcast-client-experimental/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"clean": "premove ./build ./coverage ./dist-ts",
"compile": "tsc --build",
"test:eslint": "eslint ./src",
"test:types": "pnpm run \"/^test:types:ts[0-9]{2}$/\"",
"test:types": "npm-run-all --serial test:types:*",
"test:types:ts50": "node ../../node_modules/typescript50/lib/tsc.js --build",
"test:types:ts51": "node ../../node_modules/typescript51/lib/tsc.js --build",
"test:types:ts52": "node ../../node_modules/typescript52/lib/tsc.js --build",
Expand Down Expand Up @@ -56,5 +56,8 @@
"dependencies": {
"@tanstack/query-core": "workspace:*",
"broadcast-channel": "^7.0.0"
},
"devDependencies": {
"npm-run-all": "^4.1.5"
}
}
5 changes: 3 additions & 2 deletions packages/query-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"clean": "premove ./build ./coverage ./dist-ts",
"compile": "tsc --build",
"test:eslint": "eslint ./src",
"test:types": "pnpm run \"/^test:types:ts[0-9]{2}$/\"",
"test:types": "npm-run-all --serial test:types:*",
"test:types:ts50": "node ../../node_modules/typescript50/lib/tsc.js --build tsconfig.legacy.json",
"test:types:ts51": "node ../../node_modules/typescript51/lib/tsc.js --build tsconfig.legacy.json",
"test:types:ts52": "node ../../node_modules/typescript52/lib/tsc.js --build tsconfig.legacy.json",
Expand Down Expand Up @@ -56,6 +56,7 @@
"!src/__tests__"
],
"devDependencies": {
"@testing-library/dom": "^10.4.0"
"@testing-library/dom": "^10.4.0",
"npm-run-all": "^4.1.5"
}
}
11 changes: 11 additions & 0 deletions packages/query-core/src/__tests__/infiniteQueryObserver.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,22 @@ describe('InfiniteQueryObserver', () => {
expectTypeOf(result.error).toEqualTypeOf<null>()
expectTypeOf(result.isLoading).toEqualTypeOf<boolean>()
expectTypeOf(result.status).toEqualTypeOf<'pending'>()
expectTypeOf(result.isPlaceholderData).toEqualTypeOf<false>()
}

if (result.isLoading) {
expectTypeOf(result.data).toEqualTypeOf<undefined>()
expectTypeOf(result.error).toEqualTypeOf<null>()
expectTypeOf(result.isPending).toEqualTypeOf<true>()
expectTypeOf(result.status).toEqualTypeOf<'pending'>()
expectTypeOf(result.isPlaceholderData).toEqualTypeOf<false>()
}

if (result.isLoadingError) {
expectTypeOf(result.data).toEqualTypeOf<undefined>()
expectTypeOf(result.error).toEqualTypeOf<Error>()
expectTypeOf(result.status).toEqualTypeOf<'error'>()
expectTypeOf(result.isPlaceholderData).toEqualTypeOf<false>()
}

if (result.isRefetchError) {
Expand All @@ -53,12 +56,20 @@ describe('InfiniteQueryObserver', () => {
expectTypeOf(result.status).toEqualTypeOf<'error'>()
expectTypeOf(result.isFetchNextPageError).toEqualTypeOf<boolean>()
expectTypeOf(result.isFetchPreviousPageError).toEqualTypeOf<boolean>()
expectTypeOf(result.isPlaceholderData).toEqualTypeOf<false>()
}

if (result.isSuccess) {
expectTypeOf(result.data).toEqualTypeOf<InfiniteData<string, unknown>>()
expectTypeOf(result.error).toEqualTypeOf<null>()
expectTypeOf(result.status).toEqualTypeOf<'success'>()
expectTypeOf(result.isPlaceholderData).toEqualTypeOf<boolean>()
}

if (result.isPlaceholderData) {
expectTypeOf(result.data).toEqualTypeOf<InfiniteData<string, unknown>>()
expectTypeOf(result.error).toEqualTypeOf<null>()
expectTypeOf(result.status).toEqualTypeOf<'success'>()
}
})
})
18 changes: 18 additions & 0 deletions packages/query-core/src/__tests__/queryObserver.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('queryObserver', () => {
expectTypeOf(result.isLoadingError).toEqualTypeOf<false>()
expectTypeOf(result.isRefetchError).toEqualTypeOf<false>()
expectTypeOf(result.status).toEqualTypeOf<'pending'>()
expectTypeOf(result.isPlaceholderData).toEqualTypeOf<false>()
}
if (result.isLoading) {
expectTypeOf(result.data).toEqualTypeOf<undefined>()
Expand All @@ -43,6 +44,7 @@ describe('queryObserver', () => {
expectTypeOf(result.isRefetchError).toEqualTypeOf<false>()
expectTypeOf(result.isSuccess).toEqualTypeOf<false>()
expectTypeOf(result.status).toEqualTypeOf<'pending'>()
expectTypeOf(result.isPlaceholderData).toEqualTypeOf<false>()
}

if (result.isLoadingError) {
Expand All @@ -55,6 +57,7 @@ describe('queryObserver', () => {
expectTypeOf(result.isRefetchError).toEqualTypeOf<false>()
expectTypeOf(result.isSuccess).toEqualTypeOf<false>()
expectTypeOf(result.status).toEqualTypeOf<'error'>()
expectTypeOf(result.isPlaceholderData).toEqualTypeOf<false>()
}

if (result.isRefetchError) {
Expand All @@ -67,6 +70,7 @@ describe('queryObserver', () => {
expectTypeOf(result.isRefetchError).toEqualTypeOf<true>()
expectTypeOf(result.isSuccess).toEqualTypeOf<false>()
expectTypeOf(result.status).toEqualTypeOf<'error'>()
expectTypeOf(result.isPlaceholderData).toEqualTypeOf<false>()
}

if (result.isSuccess) {
Expand All @@ -79,6 +83,20 @@ describe('queryObserver', () => {
expectTypeOf(result.isRefetchError).toEqualTypeOf<false>()
expectTypeOf(result.isSuccess).toEqualTypeOf<true>()
expectTypeOf(result.status).toEqualTypeOf<'success'>()
expectTypeOf(result.isPlaceholderData).toEqualTypeOf<boolean>()
}

if (result.isPlaceholderData) {
expectTypeOf(result.data).toEqualTypeOf<{ value: string }>()
expectTypeOf(result.error).toEqualTypeOf<null>()
expectTypeOf(result.isError).toEqualTypeOf<false>()
expectTypeOf(result.isPending).toEqualTypeOf<false>()
expectTypeOf(result.isLoading).toEqualTypeOf<false>()
expectTypeOf(result.isLoadingError).toEqualTypeOf<false>()
expectTypeOf(result.isRefetchError).toEqualTypeOf<false>()
expectTypeOf(result.isSuccess).toEqualTypeOf<true>()
expectTypeOf(result.status).toEqualTypeOf<'success'>()
expectTypeOf(result.isPlaceholderData).toEqualTypeOf<true>()
}
})

Expand Down
46 changes: 46 additions & 0 deletions packages/query-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ export interface QueryObserverPendingResult<
isLoadingError: false
isRefetchError: false
isSuccess: false
isPlaceholderData: false
status: 'pending'
}

Expand All @@ -806,6 +807,7 @@ export interface QueryObserverLoadingResult<
isLoadingError: false
isRefetchError: false
isSuccess: false
isPlaceholderData: false
status: 'pending'
}

Expand All @@ -821,6 +823,7 @@ export interface QueryObserverLoadingErrorResult<
isLoadingError: true
isRefetchError: false
isSuccess: false
isPlaceholderData: false
status: 'error'
}

Expand All @@ -836,6 +839,7 @@ export interface QueryObserverRefetchErrorResult<
isLoadingError: false
isRefetchError: true
isSuccess: false
isPlaceholderData: false
status: 'error'
}

Expand All @@ -851,6 +855,23 @@ export interface QueryObserverSuccessResult<
isLoadingError: false
isRefetchError: false
isSuccess: true
isPlaceholderData: false
status: 'success'
}

export interface QueryObserverPlaceholderResult<
TData = unknown,
TError = DefaultError,
> extends QueryObserverBaseResult<TData, TError> {
data: TData
isError: false
error: null
isPending: false
isLoading: false
isLoadingError: false
isRefetchError: false
isSuccess: true
isPlaceholderData: true
status: 'success'
}

Expand All @@ -866,6 +887,7 @@ export type QueryObserverResult<TData = unknown, TError = DefaultError> =
| QueryObserverLoadingErrorResult<TData, TError>
| QueryObserverLoadingResult<TData, TError>
| QueryObserverPendingResult<TData, TError>
| QueryObserverPlaceholderResult<TData, TError>

export interface InfiniteQueryObserverBaseResult<
TData = unknown,
Expand Down Expand Up @@ -922,6 +944,7 @@ export interface InfiniteQueryObserverPendingResult<
isFetchNextPageError: false
isFetchPreviousPageError: false
isSuccess: false
isPlaceholderData: false
status: 'pending'
}

Expand All @@ -939,6 +962,7 @@ export interface InfiniteQueryObserverLoadingResult<
isFetchNextPageError: false
isFetchPreviousPageError: false
isSuccess: false
isPlaceholderData: false
status: 'pending'
}

Expand All @@ -956,6 +980,7 @@ export interface InfiniteQueryObserverLoadingErrorResult<
isFetchNextPageError: false
isFetchPreviousPageError: false
isSuccess: false
isPlaceholderData: false
status: 'error'
}

Expand All @@ -971,6 +996,7 @@ export interface InfiniteQueryObserverRefetchErrorResult<
isLoadingError: false
isRefetchError: true
isSuccess: false
isPlaceholderData: false
status: 'error'
}

Expand All @@ -988,6 +1014,25 @@ export interface InfiniteQueryObserverSuccessResult<
isFetchNextPageError: false
isFetchPreviousPageError: false
isSuccess: true
isPlaceholderData: false
status: 'success'
}

export interface InfiniteQueryObserverPlaceholderResult<
TData = unknown,
TError = DefaultError,
> extends InfiniteQueryObserverBaseResult<TData, TError> {
data: TData
isError: false
error: null
isPending: false
isLoading: false
isLoadingError: false
isRefetchError: false
isSuccess: true
isPlaceholderData: true
isFetchNextPageError: false
isFetchPreviousPageError: false
status: 'success'
}

Expand All @@ -1006,6 +1051,7 @@ export type InfiniteQueryObserverResult<
| InfiniteQueryObserverLoadingErrorResult<TData, TError>
| InfiniteQueryObserverLoadingResult<TData, TError>
| InfiniteQueryObserverPendingResult<TData, TError>
| InfiniteQueryObserverPlaceholderResult<TData, TError>

export type MutationKey = Register extends {
mutationKey: infer TMutationKey
Expand Down
3 changes: 2 additions & 1 deletion packages/query-devtools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"clean": "premove ./build ./coverage ./dist-ts",
"compile": "tsc --build",
"test:eslint": "eslint ./src",
"test:types": "pnpm run \"/^test:types:ts[0-9]{2}$/\"",
"test:types": "npm-run-all --serial test:types:*",
"test:types:ts50": "node ../../node_modules/typescript50/lib/tsc.js --build",
"test:types:ts51": "node ../../node_modules/typescript51/lib/tsc.js --build",
"test:types:ts52": "node ../../node_modules/typescript52/lib/tsc.js --build",
Expand Down Expand Up @@ -70,6 +70,7 @@
"@tanstack/query-core": "workspace:*",
"clsx": "^2.1.1",
"goober": "^2.1.16",
"npm-run-all": "^4.1.5",
"solid-js": "^1.9.3",
"solid-transition-group": "^0.2.3",
"superjson": "^2.2.1",
Expand Down
5 changes: 4 additions & 1 deletion packages/query-persist-client-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"clean": "premove ./build ./coverage ./dist-ts",
"compile": "tsc --build",
"test:eslint": "eslint ./src",
"test:types": "pnpm run \"/^test:types:ts[0-9]{2}$/\"",
"test:types": "npm-run-all --serial test:types:*",
"test:types:ts50": "node ../../node_modules/typescript50/lib/tsc.js --build",
"test:types:ts51": "node ../../node_modules/typescript51/lib/tsc.js --build",
"test:types:ts52": "node ../../node_modules/typescript52/lib/tsc.js --build",
Expand Down Expand Up @@ -57,5 +57,8 @@
],
"dependencies": {
"@tanstack/query-core": "workspace:*"
},
"devDependencies": {
"npm-run-all": "^4.1.5"
}
}
5 changes: 4 additions & 1 deletion packages/query-sync-storage-persister/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"clean": "premove ./build ./coverage ./dist-ts",
"compile": "tsc --build",
"test:eslint": "eslint ./src",
"test:types": "pnpm run \"/^test:types:ts[0-9]{2}$/\"",
"test:types": "npm-run-all --serial test:types:*",
"test:types:ts50": "node ../../node_modules/typescript50/lib/tsc.js --build",
"test:types:ts51": "node ../../node_modules/typescript51/lib/tsc.js --build",
"test:types:ts52": "node ../../node_modules/typescript52/lib/tsc.js --build",
Expand Down Expand Up @@ -58,5 +58,8 @@
"dependencies": {
"@tanstack/query-core": "workspace:*",
"@tanstack/query-persist-client-core": "workspace:*"
},
"devDependencies": {
"npm-run-all": "^4.1.5"
}
}
Loading

0 comments on commit 2399bb0

Please sign in to comment.