Skip to content

Commit 73ccba8

Browse files
authored
feat: publish new types (TanStack#897)
1 parent ee2735d commit 73ccba8

26 files changed

+194
-2162
lines changed

.github/workflows/test-and-publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
registry-url: https://registry.npmjs.org/
3939
- name: Install dependencies
4040
uses: bahmutov/npm-install@v1
41-
- run: yarn build
41+
- run: yarn build && yarn build:types
4242
- run: npx semantic-release@17
4343
env:
4444
NODE_AUTH_TOKEN: ${{secrets.npm_token}}

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,4 @@ size-plugin.json
2828
stats.html
2929
.vscode/settings.json
3030

31-
!/types/index.d.ts
32-
types/**/*.ts
31+
types

package.json

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@
1717
"scripts": {
1818
"test": "is-ci \"test:ci\" \"test:dev\"",
1919
"test:dev": "npm run test:types && npm run test:eslint && jest --watch",
20-
"test:ci": "npm run test:types && npm run test:eslint && jest && yarn dtslint",
20+
"test:ci": "npm run test:types && npm run test:eslint && jest",
2121
"test:coverage": "yarn test:ci; open coverage/lcov-report/index.html",
2222
"test:types": "tsc",
2323
"test:eslint": "eslint --ext .ts,.tsx ./src",
24-
"gen:types": "tsc --project ./tsconfig.types.json",
2524
"build": "NODE_ENV=production rollup -c",
25+
"build:types": "tsc --project ./tsconfig.types.json && replace 'import type' 'import' ./types -r && replace 'export type' 'export' ./types -r",
2626
"now-build": "yarn && cd www && yarn && yarn build",
2727
"start": "rollup -c -w",
2828
"format": "prettier {.,src,src/**,example/src,example/src/**,types}/*.{md,js,jsx,tsx,json} --write",
29-
"stats": "open ./stats.html",
30-
"dtslint": "dtslint types"
29+
"stats": "open ./stats.html"
3130
},
3231
"release": {
3332
"branches": [
@@ -44,9 +43,7 @@
4443
"types",
4544
"scripts"
4645
],
47-
"dependencies": {
48-
"ts-toolbelt": "^6.9.4"
49-
},
46+
"dependencies": {},
5047
"peerDependencies": {
5148
"react": "^16.8.0"
5249
},
@@ -66,7 +63,6 @@
6663
"babel-jest": "^26.0.1",
6764
"babel-plugin-transform-async-to-promises": "^0.8.15",
6865
"cross-env": "^7.0.2",
69-
"dtslint": "^3.6.12",
7066
"eslint": "7.x",
7167
"eslint-config-prettier": "^6.11.0",
7268
"eslint-config-react-app": "^5.2.1",
@@ -87,6 +83,7 @@
8783
"react": "^16.13.0",
8884
"react-dom": "^16.13.1",
8985
"react-error-boundary": "^2.2.2",
86+
"replace": "^1.2.0",
9087
"rollup": "^2.16.1",
9188
"rollup-plugin-babel": "^4.4.0",
9289
"rollup-plugin-commonjs": "^10.1.0",

src/core/queryCache.ts

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ import { getDefaultedQueryConfig } from './config'
1010
import { Query } from './query'
1111
import {
1212
QueryConfig,
13+
QueryFunction,
1314
QueryKey,
14-
QueryKeyWithoutObject,
1515
ReactQueryConfig,
16-
QueryKeyWithoutArray,
17-
QueryKeyWithoutObjectAndArray,
18-
TupleQueryFunction,
19-
TupleQueryKey,
16+
TypedQueryFunction,
17+
TypedQueryFunctionArgs,
2018
} from './types'
2119

2220
// TYPES
@@ -49,13 +47,9 @@ type QueryPredicate = QueryKey | QueryPredicateFn | true
4947

5048
type QueryPredicateFn = (query: Query<unknown, unknown>) => boolean
5149

52-
export interface PrefetchQueryObjectConfig<
53-
TResult,
54-
TError,
55-
TKey extends TupleQueryKey
56-
> {
50+
export interface PrefetchQueryObjectConfig<TResult, TError> {
5751
queryKey: QueryKey
58-
queryFn?: TupleQueryFunction<TResult, TKey>
52+
queryFn?: QueryFunction<TResult>
5953
config?: QueryConfig<TResult, TError>
6054
options?: PrefetchQueryOptions
6155
}
@@ -255,61 +249,42 @@ export class QueryCache {
255249
}
256250

257251
// Parameter syntax with optional prefetch options
258-
async prefetchQuery<TResult, TError, TKey extends QueryKeyWithoutObject>(
259-
queryKey: TKey,
260-
options?: PrefetchQueryOptions
261-
): Promise<TResult | undefined>
262-
263-
// Parameter syntax with config and optional prefetch options
264-
async prefetchQuery<TResult, TError, TKey extends QueryKeyWithoutObject>(
265-
queryKey: TKey,
266-
config: QueryConfig<TResult, TError>,
252+
async prefetchQuery<TResult = unknown, TError = unknown>(
253+
queryKey: QueryKey,
267254
options?: PrefetchQueryOptions
268255
): Promise<TResult | undefined>
269256

270257
// Parameter syntax with query function and optional prefetch options
271-
async prefetchQuery<
272-
TResult,
273-
TError,
274-
TKey extends QueryKeyWithoutObjectAndArray
275-
>(
276-
queryKey: TKey,
277-
queryFn: TupleQueryFunction<TResult, [TKey]>,
258+
async prefetchQuery<TResult, TError, TArgs extends TypedQueryFunctionArgs>(
259+
queryKey: QueryKey,
260+
queryFn: TypedQueryFunction<TResult, TArgs>,
278261
options?: PrefetchQueryOptions
279262
): Promise<TResult | undefined>
280263

281-
async prefetchQuery<TResult, TError, TKey extends TupleQueryKey>(
282-
queryKey: TKey,
283-
queryFn: TupleQueryFunction<TResult, TKey>,
264+
async prefetchQuery<TResult = unknown, TError = unknown>(
265+
queryKey: QueryKey,
266+
queryFn: QueryFunction<TResult>,
284267
options?: PrefetchQueryOptions
285268
): Promise<TResult | undefined>
286269

287270
// Parameter syntax with query function, config and optional prefetch options
288-
async prefetchQuery<
289-
TResult,
290-
TError,
291-
TKey extends QueryKeyWithoutObjectAndArray
292-
>(
293-
queryKey: TKey,
294-
queryFn: TupleQueryFunction<TResult, [TKey]>,
271+
async prefetchQuery<TResult, TError, TArgs extends TypedQueryFunctionArgs>(
272+
queryKey: QueryKey,
273+
queryFn: TypedQueryFunction<TResult, TArgs>,
295274
queryConfig: QueryConfig<TResult, TError>,
296275
options?: PrefetchQueryOptions
297276
): Promise<TResult | undefined>
298277

299-
async prefetchQuery<TResult, TError, TKey extends TupleQueryKey>(
300-
queryKey: TKey,
301-
queryFn: TupleQueryFunction<TResult, TKey>,
278+
async prefetchQuery<TResult = unknown, TError = unknown>(
279+
queryKey: QueryKey,
280+
queryFn: QueryFunction<TResult>,
302281
queryConfig: QueryConfig<TResult, TError>,
303282
options?: PrefetchQueryOptions
304283
): Promise<TResult | undefined>
305284

306285
// Object syntax
307-
async prefetchQuery<TResult, TError, TKey extends QueryKeyWithoutArray>(
308-
config: PrefetchQueryObjectConfig<TResult, TError, [TKey]>
309-
): Promise<TResult | undefined>
310-
311-
async prefetchQuery<TResult, TError, TKey extends TupleQueryKey>(
312-
config: PrefetchQueryObjectConfig<TResult, TError, TKey>
286+
async prefetchQuery<TResult = unknown, TError = unknown>(
287+
config: PrefetchQueryObjectConfig<TResult, TError>
313288
): Promise<TResult | undefined>
314289

315290
// Implementation

src/core/tests/queryCache.test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { sleep, queryKey } from '../../react/tests/utils'
2-
import { queryCache as defaultQueryCache } from '../'
3-
import { makeQueryCache } from '../queryCache'
2+
import { makeQueryCache, queryCache as defaultQueryCache } from '..'
43

54
describe('queryCache', () => {
65
test('setQueryData does not crash if query could not be found', () => {

src/core/tests/utils.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { setConsole, queryCache } from '../'
21
import { deepEqual, replaceEqualDeep } from '../utils'
2+
import { setConsole, queryCache } from '..'
33
import { queryKey } from '../../react/tests/utils'
44

55
describe('core/utils', () => {

src/core/types.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,29 @@
11
import type { Query, FetchMoreOptions } from './query'
22
import type { QueryCache } from './queryCache'
33

4-
export type QueryKeyObject =
4+
export type QueryKey =
5+
| boolean
6+
| null
7+
| number
58
| object
6-
| { [key: string]: QueryKey }
9+
| string
10+
| undefined
711
| { [key: number]: QueryKey }
8-
9-
export type QueryKeyPrimitive = string | boolean | number | null | undefined
10-
11-
export type QueryKeyWithoutObjectAndArray = QueryKeyPrimitive
12-
13-
export type QueryKeyWithoutObject =
14-
| QueryKeyWithoutObjectAndArray
12+
| { [key: string]: QueryKey }
1513
| readonly QueryKey[]
1614

17-
export type QueryKeyWithoutArray =
18-
| QueryKeyWithoutObjectAndArray
19-
| QueryKeyObject
20-
21-
export type QueryKey = QueryKeyWithoutObject | QueryKeyObject
22-
2315
export type ArrayQueryKey = QueryKey[]
2416

2517
export type QueryFunction<TResult> = (
2618
...args: any[]
2719
) => TResult | Promise<TResult>
2820

29-
// The tuple variants are only to infer types in the public API
30-
export type TupleQueryKey = readonly [QueryKey, ...QueryKey[]]
21+
export type TypedQueryFunction<
22+
TResult,
23+
TArgs extends TypedQueryFunctionArgs = TypedQueryFunctionArgs
24+
> = (...args: TArgs) => TResult | Promise<TResult>
3125

32-
export type TupleQueryFunction<TResult, TKey extends TupleQueryKey> = (
33-
...args: TKey
34-
) => TResult | Promise<TResult>
26+
export type TypedQueryFunctionArgs = readonly [unknown, ...unknown[]]
3527

3628
export type InitialDataFunction<TResult> = () => TResult | undefined
3729

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * from './core/index'
12
export * from './react/index'

src/react/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
export * from '../core/index'
2-
3-
// React
41
export {
52
ReactQueryCacheProvider,
63
useQueryCache,

src/react/tests/ReactQueryCacheProvider.test.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import React, { useEffect } from 'react'
22
import { render, waitFor } from '@testing-library/react'
3-
import {
4-
ReactQueryCacheProvider,
5-
makeQueryCache,
6-
queryCache,
7-
useQuery,
8-
useQueryCache,
9-
} from '../index'
3+
104
import { sleep, queryKey } from './utils'
11-
import { QueryCache } from '../../core/queryCache'
5+
import { ReactQueryCacheProvider, useQuery, useQueryCache } from '..'
6+
import { makeQueryCache, queryCache, QueryCache } from '../../core'
127

138
describe('ReactQueryCacheProvider', () => {
149
test('when not used, falls back to global cache', async () => {

0 commit comments

Comments
 (0)