@tanstack/react-router 1.22.4 → 1.22.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/CatchBoundary.cjs +1 -2
- package/dist/cjs/CatchBoundary.cjs.map +1 -1
- package/dist/cjs/Matches.cjs +15 -15
- package/dist/cjs/Matches.cjs.map +1 -1
- package/dist/cjs/Matches.d.cts +15 -15
- package/dist/cjs/RouterProvider.cjs +16 -15
- package/dist/cjs/RouterProvider.cjs.map +1 -1
- package/dist/cjs/RouterProvider.d.cts +6 -6
- package/dist/cjs/awaited.cjs +1 -1
- package/dist/cjs/awaited.cjs.map +1 -1
- package/dist/cjs/awaited.d.cts +1 -1
- package/dist/cjs/defer.cjs.map +1 -1
- package/dist/cjs/fileRoute.cjs +1 -1
- package/dist/cjs/fileRoute.cjs.map +1 -1
- package/dist/cjs/fileRoute.d.cts +17 -17
- package/dist/cjs/lazyRouteComponent.cjs.map +1 -1
- package/dist/cjs/lazyRouteComponent.d.cts +1 -1
- package/dist/cjs/link.cjs +10 -11
- package/dist/cjs/link.cjs.map +1 -1
- package/dist/cjs/link.d.cts +16 -13
- package/dist/cjs/location.d.cts +2 -2
- package/dist/cjs/not-found.cjs.map +1 -1
- package/dist/cjs/not-found.d.cts +2 -2
- package/dist/cjs/path.cjs +2 -2
- package/dist/cjs/path.cjs.map +1 -1
- package/dist/cjs/path.d.cts +4 -4
- package/dist/cjs/qss.cjs +4 -3
- package/dist/cjs/qss.cjs.map +1 -1
- package/dist/cjs/redirects.cjs.map +1 -1
- package/dist/cjs/redirects.d.cts +5 -5
- package/dist/cjs/route.cjs.map +1 -1
- package/dist/cjs/route.d.cts +34 -36
- package/dist/cjs/routeInfo.d.cts +5 -5
- package/dist/cjs/router.cjs +63 -66
- package/dist/cjs/router.cjs.map +1 -1
- package/dist/cjs/router.d.cts +28 -28
- package/dist/cjs/routerContext.cjs +1 -1
- package/dist/cjs/routerContext.cjs.map +1 -1
- package/dist/cjs/routerContext.d.cts +1 -1
- package/dist/cjs/scroll-restoration.cjs +7 -9
- package/dist/cjs/scroll-restoration.cjs.map +1 -1
- package/dist/cjs/scroll-restoration.d.cts +1 -1
- package/dist/cjs/searchParams.cjs +10 -12
- package/dist/cjs/searchParams.cjs.map +1 -1
- package/dist/cjs/searchParams.d.cts +1 -1
- package/dist/cjs/useBlocker.cjs.map +1 -1
- package/dist/cjs/useBlocker.d.cts +2 -2
- package/dist/cjs/useNavigate.cjs +1 -1
- package/dist/cjs/useNavigate.cjs.map +1 -1
- package/dist/cjs/useNavigate.d.cts +4 -4
- package/dist/cjs/useParams.cjs +1 -1
- package/dist/cjs/useParams.cjs.map +1 -1
- package/dist/cjs/useParams.d.cts +5 -5
- package/dist/cjs/useRouteContext.cjs +1 -1
- package/dist/cjs/useRouteContext.cjs.map +1 -1
- package/dist/cjs/useRouteContext.d.cts +4 -4
- package/dist/cjs/useRouter.cjs.map +1 -1
- package/dist/cjs/useRouter.d.cts +2 -2
- package/dist/cjs/useRouterState.cjs.map +1 -1
- package/dist/cjs/useRouterState.d.cts +2 -2
- package/dist/cjs/useSearch.cjs +1 -1
- package/dist/cjs/useSearch.cjs.map +1 -1
- package/dist/cjs/useSearch.d.cts +4 -4
- package/dist/cjs/utils.cjs +2 -2
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +13 -11
- package/dist/esm/CatchBoundary.js +1 -2
- package/dist/esm/CatchBoundary.js.map +1 -1
- package/dist/esm/Matches.d.ts +15 -15
- package/dist/esm/Matches.js +15 -15
- package/dist/esm/Matches.js.map +1 -1
- package/dist/esm/RouterProvider.d.ts +6 -6
- package/dist/esm/RouterProvider.js +16 -15
- package/dist/esm/RouterProvider.js.map +1 -1
- package/dist/esm/awaited.d.ts +1 -1
- package/dist/esm/awaited.js +1 -1
- package/dist/esm/awaited.js.map +1 -1
- package/dist/esm/defer.js.map +1 -1
- package/dist/esm/fileRoute.d.ts +17 -17
- package/dist/esm/fileRoute.js +1 -1
- package/dist/esm/fileRoute.js.map +1 -1
- package/dist/esm/lazyRouteComponent.d.ts +1 -1
- package/dist/esm/lazyRouteComponent.js.map +1 -1
- package/dist/esm/link.d.ts +16 -13
- package/dist/esm/link.js +10 -11
- package/dist/esm/link.js.map +1 -1
- package/dist/esm/location.d.ts +2 -2
- package/dist/esm/not-found.d.ts +2 -2
- package/dist/esm/not-found.js.map +1 -1
- package/dist/esm/path.d.ts +4 -4
- package/dist/esm/path.js +2 -2
- package/dist/esm/path.js.map +1 -1
- package/dist/esm/qss.js +4 -3
- package/dist/esm/qss.js.map +1 -1
- package/dist/esm/redirects.d.ts +5 -5
- package/dist/esm/redirects.js.map +1 -1
- package/dist/esm/route.d.ts +34 -36
- package/dist/esm/route.js.map +1 -1
- package/dist/esm/routeInfo.d.ts +5 -5
- package/dist/esm/router.d.ts +28 -28
- package/dist/esm/router.js +63 -66
- package/dist/esm/router.js.map +1 -1
- package/dist/esm/routerContext.d.ts +1 -1
- package/dist/esm/routerContext.js +1 -1
- package/dist/esm/routerContext.js.map +1 -1
- package/dist/esm/scroll-restoration.d.ts +1 -1
- package/dist/esm/scroll-restoration.js +7 -9
- package/dist/esm/scroll-restoration.js.map +1 -1
- package/dist/esm/searchParams.d.ts +1 -1
- package/dist/esm/searchParams.js +10 -12
- package/dist/esm/searchParams.js.map +1 -1
- package/dist/esm/useBlocker.d.ts +2 -2
- package/dist/esm/useBlocker.js.map +1 -1
- package/dist/esm/useNavigate.d.ts +4 -4
- package/dist/esm/useNavigate.js +1 -1
- package/dist/esm/useNavigate.js.map +1 -1
- package/dist/esm/useParams.d.ts +5 -5
- package/dist/esm/useParams.js +1 -1
- package/dist/esm/useParams.js.map +1 -1
- package/dist/esm/useRouteContext.d.ts +4 -4
- package/dist/esm/useRouteContext.js +1 -1
- package/dist/esm/useRouteContext.js.map +1 -1
- package/dist/esm/useRouter.d.ts +2 -2
- package/dist/esm/useRouter.js.map +1 -1
- package/dist/esm/useRouterState.d.ts +2 -2
- package/dist/esm/useRouterState.js.map +1 -1
- package/dist/esm/useSearch.d.ts +4 -4
- package/dist/esm/useSearch.js +1 -1
- package/dist/esm/useSearch.js.map +1 -1
- package/dist/esm/utils.d.ts +13 -11
- package/dist/esm/utils.js +2 -2
- package/dist/esm/utils.js.map +1 -1
- package/package.json +3 -2
- package/src/CatchBoundary.tsx +1 -1
- package/src/Matches.tsx +48 -47
- package/src/RouterProvider.tsx +22 -16
- package/src/awaited.tsx +3 -3
- package/src/defer.ts +1 -0
- package/src/fileRoute.ts +53 -53
- package/src/history.ts +1 -1
- package/src/lazyRouteComponent.tsx +2 -1
- package/src/link.tsx +50 -42
- package/src/location.ts +2 -2
- package/src/not-found.tsx +3 -2
- package/src/path.ts +8 -8
- package/src/qss.ts +4 -5
- package/src/redirects.ts +5 -5
- package/src/route.ts +73 -67
- package/src/routeInfo.ts +8 -6
- package/src/router.ts +150 -145
- package/src/routerContext.tsx +2 -2
- package/src/scroll-restoration.tsx +9 -12
- package/src/searchParams.ts +11 -13
- package/src/useBlocker.tsx +3 -3
- package/src/useNavigate.tsx +7 -5
- package/src/useParams.tsx +6 -6
- package/src/useRouteContext.ts +7 -8
- package/src/useRouter.tsx +2 -2
- package/src/useRouterState.tsx +2 -2
- package/src/useSearch.tsx +7 -6
- package/src/utils.ts +36 -24
package/src/routerContext.tsx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as React from 'react'
|
|
2
|
-
import { Router } from './router'
|
|
2
|
+
import type { Router } from './router'
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
const routerContext = React.createContext<Router<any>>(null!)
|
|
5
5
|
|
|
6
6
|
export function getRouterContext() {
|
|
7
7
|
if (typeof document === 'undefined') {
|
|
@@ -3,9 +3,10 @@ import * as React from 'react'
|
|
|
3
3
|
const useLayoutEffect =
|
|
4
4
|
typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect
|
|
5
5
|
|
|
6
|
-
import { ParsedLocation } from './location'
|
|
7
6
|
import { useRouter } from './useRouter'
|
|
8
|
-
import {
|
|
7
|
+
import { functionalUpdate } from './utils'
|
|
8
|
+
import type { ParsedLocation } from './location'
|
|
9
|
+
import type { NonNullableUpdater } from './utils'
|
|
9
10
|
|
|
10
11
|
const windowKey = 'window'
|
|
11
12
|
const delimiter = '___'
|
|
@@ -25,7 +26,7 @@ type Cache = {
|
|
|
25
26
|
|
|
26
27
|
const sessionsStorage = typeof window !== 'undefined' && window.sessionStorage
|
|
27
28
|
|
|
28
|
-
|
|
29
|
+
const cache: Cache = sessionsStorage
|
|
29
30
|
? (() => {
|
|
30
31
|
const storageKey = 'tsr-scroll-restoration-v2'
|
|
31
32
|
|
|
@@ -56,9 +57,7 @@ export function useScrollRestoration(options?: ScrollRestorationOptions) {
|
|
|
56
57
|
const getKey = options?.getKey || defaultGetKey
|
|
57
58
|
|
|
58
59
|
const { history } = window
|
|
59
|
-
|
|
60
|
-
history.scrollRestoration = 'manual'
|
|
61
|
-
}
|
|
60
|
+
history.scrollRestoration = 'manual'
|
|
62
61
|
|
|
63
62
|
const onScroll = (event: Event) => {
|
|
64
63
|
if (weakScrolledElements.has(event.target)) return
|
|
@@ -137,8 +136,6 @@ export function useScrollRestoration(options?: ScrollRestorationOptions) {
|
|
|
137
136
|
|
|
138
137
|
router.resetNextScroll = true
|
|
139
138
|
|
|
140
|
-
const getKey = options?.getKey || defaultGetKey
|
|
141
|
-
|
|
142
139
|
const restoreKey = getKey(event.toLocation)
|
|
143
140
|
let windowRestored = false
|
|
144
141
|
|
|
@@ -173,7 +170,7 @@ export function useScrollRestoration(options?: ScrollRestorationOptions) {
|
|
|
173
170
|
unsubOnBeforeLoad()
|
|
174
171
|
unsubOnResolved()
|
|
175
172
|
}
|
|
176
|
-
}, [])
|
|
173
|
+
}, [options?.getKey, router])
|
|
177
174
|
}
|
|
178
175
|
|
|
179
176
|
export function ScrollRestoration(props: ScrollRestorationOptions) {
|
|
@@ -196,7 +193,7 @@ export function useElementScrollRestoration(
|
|
|
196
193
|
},
|
|
197
194
|
) {
|
|
198
195
|
const router = useRouter()
|
|
199
|
-
const getKey = options
|
|
196
|
+
const getKey = options.getKey || defaultGetKey
|
|
200
197
|
|
|
201
198
|
let elementSelector = ''
|
|
202
199
|
|
|
@@ -216,8 +213,8 @@ export function useElementScrollRestoration(
|
|
|
216
213
|
}
|
|
217
214
|
|
|
218
215
|
function getCssSelector(el: any): string {
|
|
219
|
-
|
|
220
|
-
|
|
216
|
+
const path = []
|
|
217
|
+
let parent
|
|
221
218
|
while ((parent = el.parentNode)) {
|
|
222
219
|
path.unshift(
|
|
223
220
|
`${el.tagName}:nth-child(${
|
package/src/searchParams.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { decode, encode } from './qss'
|
|
2
|
-
import { AnySearchSchema } from './route'
|
|
2
|
+
import type { AnySearchSchema } from './route'
|
|
3
3
|
|
|
4
4
|
export const defaultParseSearch = parseSearchWith(JSON.parse)
|
|
5
5
|
export const defaultStringifySearch = stringifySearchWith(
|
|
@@ -13,10 +13,10 @@ export function parseSearchWith(parser: (str: string) => any) {
|
|
|
13
13
|
searchStr = searchStr.substring(1)
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
const query: Record<string, unknown> = decode(searchStr)
|
|
17
17
|
|
|
18
18
|
// Try to parse any query params that might be json
|
|
19
|
-
for (
|
|
19
|
+
for (const key in query) {
|
|
20
20
|
const value = query[key]
|
|
21
21
|
if (typeof value === 'string') {
|
|
22
22
|
try {
|
|
@@ -58,16 +58,14 @@ export function stringifySearchWith(
|
|
|
58
58
|
return (search: Record<string, any>) => {
|
|
59
59
|
search = { ...search }
|
|
60
60
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
})
|
|
70
|
-
}
|
|
61
|
+
Object.keys(search).forEach((key) => {
|
|
62
|
+
const val = search[key]
|
|
63
|
+
if (typeof val === 'undefined' || val === undefined) {
|
|
64
|
+
delete search[key]
|
|
65
|
+
} else {
|
|
66
|
+
search[key] = stringifyValue(val)
|
|
67
|
+
}
|
|
68
|
+
})
|
|
71
69
|
|
|
72
70
|
const searchStr = encode(search as Record<string, string>).toString()
|
|
73
71
|
|
package/src/useBlocker.tsx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as React from 'react'
|
|
2
|
-
import { ReactNode } from './route'
|
|
3
2
|
import { useRouter } from './useRouter'
|
|
4
|
-
import { BlockerFn } from '@tanstack/history'
|
|
3
|
+
import type { BlockerFn } from '@tanstack/history'
|
|
4
|
+
import type { ReactNode } from './route'
|
|
5
5
|
|
|
6
6
|
export function useBlocker(
|
|
7
7
|
blockerFn: BlockerFn,
|
|
@@ -17,7 +17,7 @@ export function useBlocker(
|
|
|
17
17
|
|
|
18
18
|
export function Block({ blocker, condition, children }: PromptProps) {
|
|
19
19
|
useBlocker(blocker, condition)
|
|
20
|
-
return
|
|
20
|
+
return children ?? null
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
export type PromptProps = {
|
package/src/useNavigate.tsx
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import * as React from 'react'
|
|
2
2
|
import { useMatch } from './Matches'
|
|
3
3
|
import { useRouter } from './useRouter'
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
4
|
+
|
|
5
|
+
import type { NavigateOptions } from './link'
|
|
6
|
+
import type { AnyRoute } from './route'
|
|
7
|
+
import type { RoutePaths, RoutePathsAutoComplete } from './routeInfo'
|
|
8
|
+
import type { RegisteredRouter } from './router'
|
|
8
9
|
|
|
9
10
|
export type UseNavigateResult<TDefaultFrom extends string> = <
|
|
10
11
|
TTo extends string,
|
|
@@ -28,7 +29,7 @@ export function useNavigate<
|
|
|
28
29
|
(options: NavigateOptions) => {
|
|
29
30
|
return router.navigate({
|
|
30
31
|
...options,
|
|
31
|
-
from: options
|
|
32
|
+
from: options.to ? router.state.resolvedLocation.pathname : undefined,
|
|
32
33
|
})
|
|
33
34
|
},
|
|
34
35
|
[router],
|
|
@@ -65,6 +66,7 @@ export function Navigate<
|
|
|
65
66
|
from: props.to ? match.pathname : undefined,
|
|
66
67
|
...props,
|
|
67
68
|
} as any)
|
|
69
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
68
70
|
}, [])
|
|
69
71
|
|
|
70
72
|
return null
|
package/src/useParams.tsx
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { AnyRoute } from './route'
|
|
2
|
-
import { RouteIds, RouteById, AllParams } from './routeInfo'
|
|
3
|
-
import { RegisteredRouter } from './router'
|
|
4
|
-
import { Expand } from './utils'
|
|
5
|
-
import { StrictOrFrom } from './utils'
|
|
6
1
|
import { useMatch } from './Matches'
|
|
2
|
+
import type { AnyRoute } from './route'
|
|
3
|
+
import type { AllParams, RouteById, RouteIds } from './routeInfo'
|
|
4
|
+
import type { RegisteredRouter } from './router'
|
|
5
|
+
import type { Expand } from './utils'
|
|
6
|
+
import type { StrictOrFrom } from './utils'
|
|
7
7
|
|
|
8
8
|
export function useParams<
|
|
9
9
|
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
@@ -21,7 +21,7 @@ export function useParams<
|
|
|
21
21
|
return useMatch({
|
|
22
22
|
...opts,
|
|
23
23
|
select: (match) => {
|
|
24
|
-
return opts
|
|
24
|
+
return opts.select ? opts.select(match.params) : match.params
|
|
25
25
|
},
|
|
26
26
|
})
|
|
27
27
|
}
|
package/src/useRouteContext.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { useMatch
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import { useMatch } from './Matches'
|
|
2
|
+
import type { RouteMatch } from './Matches'
|
|
3
|
+
import type { AnyRoute } from './route'
|
|
4
|
+
import type { RouteById, RouteIds } from './routeInfo'
|
|
5
|
+
import type { RegisteredRouter } from './router'
|
|
6
|
+
import type { StrictOrFrom } from './utils'
|
|
6
7
|
|
|
7
8
|
export function useRouteContext<
|
|
8
9
|
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
@@ -17,8 +18,6 @@ export function useRouteContext<
|
|
|
17
18
|
return useMatch({
|
|
18
19
|
...(opts as any),
|
|
19
20
|
select: (match: RouteMatch) =>
|
|
20
|
-
opts
|
|
21
|
-
? opts.select(match.context as TRouteContext)
|
|
22
|
-
: match.context,
|
|
21
|
+
opts.select ? opts.select(match.context as TRouteContext) : match.context,
|
|
23
22
|
})
|
|
24
23
|
}
|
package/src/useRouter.tsx
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as React from 'react'
|
|
2
2
|
import warning from 'tiny-warning'
|
|
3
|
-
import { AnyRoute } from './route'
|
|
4
|
-
import { RegisteredRouter, Router } from './router'
|
|
5
3
|
import { getRouterContext } from './routerContext'
|
|
4
|
+
import type { AnyRoute } from './route'
|
|
5
|
+
import type { RegisteredRouter, Router } from './router'
|
|
6
6
|
|
|
7
7
|
export function useRouter<
|
|
8
8
|
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
package/src/useRouterState.tsx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useStore } from '@tanstack/react-store'
|
|
2
|
-
import { AnyRoute } from './route'
|
|
3
|
-
import { RegisteredRouter, Router, RouterState } from './router'
|
|
4
2
|
import { useRouter } from './useRouter'
|
|
3
|
+
import type { AnyRoute } from './route'
|
|
4
|
+
import type { RegisteredRouter, Router, RouterState } from './router'
|
|
5
5
|
|
|
6
6
|
export function useRouterState<
|
|
7
7
|
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
package/src/useSearch.tsx
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { AnyRoute, RootSearchSchema } from './route'
|
|
2
|
-
import { RouteIds, RouteById, FullSearchSchema } from './routeInfo'
|
|
3
|
-
import { RegisteredRouter } from './router'
|
|
4
|
-
import { RouteMatch } from './Matches'
|
|
5
1
|
import { useMatch } from './Matches'
|
|
6
|
-
import { Expand
|
|
2
|
+
import { Expand } from './utils'
|
|
3
|
+
import type { AnyRoute, RootSearchSchema } from './route'
|
|
4
|
+
import type { FullSearchSchema, RouteById, RouteIds } from './routeInfo'
|
|
5
|
+
import type { RegisteredRouter } from './router'
|
|
6
|
+
import type { RouteMatch } from './Matches'
|
|
7
|
+
import type { StrictOrFrom } from './utils'
|
|
7
8
|
|
|
8
9
|
export function useSearch<
|
|
9
10
|
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
@@ -24,7 +25,7 @@ export function useSearch<
|
|
|
24
25
|
return useMatch({
|
|
25
26
|
...opts,
|
|
26
27
|
select: (match: RouteMatch) => {
|
|
27
|
-
return opts
|
|
28
|
+
return opts.select ? opts.select(match.search as TSearch) : match.search
|
|
28
29
|
},
|
|
29
30
|
})
|
|
30
31
|
}
|
package/src/utils.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import * as React from 'react'
|
|
2
2
|
|
|
3
3
|
export type NoInfer<T> = [T][T extends any ? 0 : never]
|
|
4
|
-
export type IsAny<
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
export type IsAny<TValue, TYesResult, TNoResult = TValue> = 1 extends 0 & TValue
|
|
5
|
+
? TYesResult
|
|
6
|
+
: TNoResult
|
|
7
|
+
export type PickAsRequired<TValue, TKey extends keyof TValue> = Omit<
|
|
8
|
+
TValue,
|
|
9
|
+
TKey
|
|
10
|
+
> &
|
|
11
|
+
Required<Pick<TValue, TKey>>
|
|
7
12
|
|
|
8
13
|
export type PickRequired<T> = {
|
|
9
14
|
[K in keyof T as undefined extends T[K] ? never : K]: T[K]
|
|
@@ -19,26 +24,25 @@ export type Expand<T> = T extends object
|
|
|
19
24
|
: never
|
|
20
25
|
: T
|
|
21
26
|
|
|
22
|
-
export type UnionToIntersection<
|
|
23
|
-
|
|
27
|
+
export type UnionToIntersection<T> = (
|
|
28
|
+
T extends any ? (k: T) => void : never
|
|
24
29
|
) extends (k: infer I) => any
|
|
25
30
|
? I
|
|
26
31
|
: never
|
|
27
32
|
|
|
28
|
-
export type DeepOptional<T, K extends keyof T> = Pick<DeepPartial<T>, K> &
|
|
29
|
-
Omit<T, K>
|
|
30
|
-
|
|
31
33
|
export type DeepPartial<T> = T extends object
|
|
32
34
|
? {
|
|
33
35
|
[P in keyof T]?: DeepPartial<T[P]>
|
|
34
36
|
}
|
|
35
37
|
: T
|
|
36
38
|
|
|
39
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
37
40
|
export type MakeDifferenceOptional<T, U> = Omit<U, keyof T> &
|
|
38
41
|
Partial<Pick<U, keyof T & keyof U>> &
|
|
39
42
|
PickRequired<Omit<U, keyof PickRequired<T>>>
|
|
40
43
|
|
|
41
44
|
// from https://stackoverflow.com/a/53955431
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
42
46
|
export type IsUnion<T, U extends T = T> = (
|
|
43
47
|
T extends any ? (U extends T ? false : true) : never
|
|
44
48
|
) extends false
|
|
@@ -61,10 +65,13 @@ export type IsUnion<T, U extends T = T> = (
|
|
|
61
65
|
// }
|
|
62
66
|
// >
|
|
63
67
|
|
|
64
|
-
export type Assign<
|
|
68
|
+
export type Assign<TLeft, TRight> = Omit<TLeft, keyof TRight> & TRight
|
|
65
69
|
|
|
66
|
-
export type AssignAll<T extends any
|
|
67
|
-
|
|
70
|
+
export type AssignAll<T extends Array<any>> = T extends [
|
|
71
|
+
infer Left,
|
|
72
|
+
...infer Right,
|
|
73
|
+
]
|
|
74
|
+
? Right extends Array<any>
|
|
68
75
|
? Assign<Left, AssignAll<Right>>
|
|
69
76
|
: Left
|
|
70
77
|
: {}
|
|
@@ -116,21 +123,21 @@ export type NonNullableUpdater<TPrevious, TResult = TPrevious> =
|
|
|
116
123
|
| ((prev: TPrevious) => TResult)
|
|
117
124
|
|
|
118
125
|
// from https://github.com/type-challenges/type-challenges/issues/737
|
|
119
|
-
type LastInUnion<
|
|
120
|
-
UnionToIntersection<
|
|
126
|
+
type LastInUnion<T> =
|
|
127
|
+
UnionToIntersection<T extends unknown ? (x: T) => 0 : never> extends (
|
|
121
128
|
x: infer L,
|
|
122
129
|
) => 0
|
|
123
130
|
? L
|
|
124
131
|
: never
|
|
125
|
-
export type UnionToTuple<
|
|
132
|
+
export type UnionToTuple<T, TLast = LastInUnion<T>> = [T] extends [never]
|
|
126
133
|
? []
|
|
127
|
-
: [...UnionToTuple<Exclude<
|
|
134
|
+
: [...UnionToTuple<Exclude<T, TLast>>, TLast]
|
|
128
135
|
|
|
129
136
|
//
|
|
130
137
|
|
|
131
138
|
export const isServer = typeof document === 'undefined'
|
|
132
139
|
|
|
133
|
-
export function last<T>(arr: T
|
|
140
|
+
export function last<T>(arr: Array<T>) {
|
|
134
141
|
return arr[arr.length - 1]
|
|
135
142
|
}
|
|
136
143
|
|
|
@@ -143,14 +150,17 @@ export function functionalUpdate<TResult>(
|
|
|
143
150
|
previous: TResult,
|
|
144
151
|
): TResult {
|
|
145
152
|
if (isFunction(updater)) {
|
|
146
|
-
return updater(previous
|
|
153
|
+
return updater(previous)
|
|
147
154
|
}
|
|
148
155
|
|
|
149
156
|
return updater
|
|
150
157
|
}
|
|
151
158
|
|
|
152
|
-
export function pick<
|
|
153
|
-
|
|
159
|
+
export function pick<TValue, TKey extends keyof TValue>(
|
|
160
|
+
parent: TValue,
|
|
161
|
+
keys: Array<TKey>,
|
|
162
|
+
): Pick<TValue, TKey> {
|
|
163
|
+
return keys.reduce((obj: any, key: TKey) => {
|
|
154
164
|
obj[key] = parent[key]
|
|
155
165
|
return obj
|
|
156
166
|
}, {} as any)
|
|
@@ -268,11 +278,13 @@ export function deepEqual(a: any, b: any, partial: boolean = false): boolean {
|
|
|
268
278
|
return false
|
|
269
279
|
}
|
|
270
280
|
|
|
271
|
-
export function useStableCallback<T extends (...args: any
|
|
281
|
+
export function useStableCallback<T extends (...args: Array<any>) => any>(
|
|
282
|
+
fn: T,
|
|
283
|
+
): T {
|
|
272
284
|
const fnRef = React.useRef(fn)
|
|
273
285
|
fnRef.current = fn
|
|
274
286
|
|
|
275
|
-
const ref = React.useRef((...args: any
|
|
287
|
+
const ref = React.useRef((...args: Array<any>) => fnRef.current(...args))
|
|
276
288
|
return ref.current as T
|
|
277
289
|
}
|
|
278
290
|
|
|
@@ -295,10 +307,10 @@ export function shallow<T>(objA: T, objB: T) {
|
|
|
295
307
|
return false
|
|
296
308
|
}
|
|
297
309
|
|
|
298
|
-
for (
|
|
310
|
+
for (const item of keysA) {
|
|
299
311
|
if (
|
|
300
|
-
!Object.prototype.hasOwnProperty.call(objB,
|
|
301
|
-
!Object.is(objA[
|
|
312
|
+
!Object.prototype.hasOwnProperty.call(objB, item) ||
|
|
313
|
+
!Object.is(objA[item as keyof T], objB[item as keyof T])
|
|
302
314
|
) {
|
|
303
315
|
return false
|
|
304
316
|
}
|