File tree Expand file tree Collapse file tree 3 files changed +35
-2
lines changed
packages/route-pattern/src/lib Expand file tree Collapse file tree 3 files changed +35
-2
lines changed Original file line number Diff line number Diff line change 11import type { RequiredParams , OptionalParams } from './params.ts'
22import { parse , type ParseResult , type Token } from './parse.ts'
33import type { RoutePattern } from './route-pattern.ts'
4+ import type { UnknownArgs } from './type-utils.ts'
45import type { Variant } from './variant.ts'
56
67/**
@@ -28,7 +29,7 @@ export class MissingParamError extends Error {
2829 * @return A function that builds hrefs from patterns and parameters
2930 */
3031export function createHrefBuilder < T extends string | RoutePattern = string > ( ) : HrefBuilder < T > {
31- return ( pattern : string | RoutePattern , ...args : any ) =>
32+ return ( pattern : string | RoutePattern , ...args : UnknownArgs ) =>
3233 formatHref ( parse ( typeof pattern === 'string' ? pattern : pattern . source ) , ...args )
3334}
3435
Original file line number Diff line number Diff line change 11import { split , type SplitPattern , type Split } from './split.ts'
22import { parseSearchConstraints , type SearchConstraints } from './search-constraints.ts'
3+ import type { ForceDistributive } from './type-utils.ts'
34
45/**
56 * An error thrown when a pattern fails to parse.
@@ -176,7 +177,7 @@ export interface ParsedPattern {
176177
177178// prettier-ignore
178179export type Parse < T extends string > =
179- T extends any ?
180+ T extends ForceDistributive ?
180181 Split < T > extends infer S extends SplitPattern ?
181182 {
182183 protocol : S [ 'protocol' ] extends string ? ParsePart < S [ 'protocol' ] > : undefined
Original file line number Diff line number Diff line change @@ -4,3 +4,34 @@ export type IsEqual<A, B> =
44 ( < T > ( ) => T extends A ? 1 : 2 ) extends < T > ( ) => T extends B ? 1 : 2 ? true : false
55
66export type Simplify < T > = { [ K in keyof T ] : T [ K ] } & { }
7+
8+ /**
9+ * Function arguments are contravariant, so unknown args must be typed as `Array<any>`
10+ *
11+ * Usage:
12+ *
13+ * ```ts
14+ * type UnknownFunction = (args: UnknownArgs) => unknown
15+ * ```
16+ */
17+ export type UnknownArgs = Array < any >
18+
19+ /**
20+ * Force TS to distribute a union with `T extends ForceDistributeUnion ? ... : ...`
21+ * as a more explicit alias of the common `T extends any ? ... : ...` pattern.
22+ *
23+ * See: https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types
24+ *
25+ * Usage:
26+ *
27+ * ```ts
28+ * type Stuff<T> =
29+ * T extends ForceDistributive ?
30+ * // Now, operate on each member of the union separately
31+ * string extends T ? 'string' :
32+ * T
33+ * :
34+ * never
35+ * ```
36+ */
37+ export type ForceDistributive = any
You can’t perform that action at this time.
0 commit comments