Skip to content

Commit c06a94e

Browse files
committed
route-pattern: explicit aliases for legitimate uses of any
1 parent 53b31dd commit c06a94e

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

packages/route-pattern/src/lib/href.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { RequiredParams, OptionalParams } from './params.ts'
22
import { parse, type ParseResult, type Token } from './parse.ts'
33
import type { RoutePattern } from './route-pattern.ts'
4+
import type { UnknownArgs } from './type-utils.ts'
45
import 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
*/
3031
export 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

packages/route-pattern/src/lib/parse.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { split, type SplitPattern, type Split } from './split.ts'
22
import { 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
178179
export 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

packages/route-pattern/src/lib/type-utils.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff 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

66
export 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

0 commit comments

Comments
 (0)