Skip to content

Commit

Permalink
Fixed type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
JonahPlusPlus committed May 28, 2024
1 parent b6b5071 commit 3e38ab6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
32 changes: 25 additions & 7 deletions packages/wouter-preact/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ export * from "./router.js";

import { RouteParams } from "regexparam";

export type RegexRouteParams = { [key: string | number]: string | undefined };

/**
* Route patterns and parameters
*/
export interface DefaultParams {
readonly [paramName: string]: string | undefined;
readonly [paramName: string | number]: string | undefined;
}

export type Params<T extends DefaultParams = DefaultParams> = T;
Expand All @@ -57,23 +59,33 @@ export interface RouteComponentProps<T extends DefaultParams = DefaultParams> {

export interface RouteProps<
T extends DefaultParams | undefined = undefined,
RoutePath extends Path = Path
RoutePath extends Path | RegExp = Path | RegExp
> {
children?:
| ((
params: T extends DefaultParams ? T : RouteParams<RoutePath>
params: T extends DefaultParams
? T
: RoutePath extends string
? RouteParams<RoutePath>
: RegexRouteParams
) => ComponentChildren)
| ComponentChildren;
path?: RoutePath;
component?: ComponentType<
RouteComponentProps<T extends DefaultParams ? T : RouteParams<RoutePath>>
RouteComponentProps<
T extends DefaultParams
? T
: RoutePath extends string
? RouteParams<RoutePath>
: RegexRouteParams
>
>;
nest?: boolean;
}

export function Route<
T extends DefaultParams | undefined = undefined,
RoutePath extends Path = Path
RoutePath extends Path | RegExp = Path | RegExp
>(props: RouteProps<T, RoutePath>): ReturnType<FunctionComponent>;

/*
Expand Down Expand Up @@ -143,10 +155,16 @@ export function useRouter(): RouterObject;

export function useRoute<
T extends DefaultParams | undefined = undefined,
RoutePath extends Path = Path
RoutePath extends Path | RegExp = Path | RegExp
>(
pattern: RoutePath
): Match<T extends DefaultParams ? T : RouteParams<RoutePath>>;
): Match<
T extends DefaultParams
? T
: RoutePath extends string
? RouteParams<RoutePath>
: RegexRouteParams
>;

export function useLocation<
H extends BaseLocationHook = BrowserLocationHook
Expand Down
5 changes: 4 additions & 1 deletion packages/wouter-preact/types/router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import {
BaseSearchHook,
} from "./location-hook.js";

export type Parser = (route: Path) => { pattern: RegExp; keys: string[] };
export type Parser = (
route: Path | RegExp,
loose?: boolean
) => { pattern: RegExp; keys: string[] };

export type HrefsFormatter = (href: string, router: RouterObject) => string;

Expand Down
2 changes: 1 addition & 1 deletion packages/wouter/test/parser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Router, useRouter, useRoute, Parser } from "wouter";
import { memoryLocation } from "wouter/memory-location";

// Custom parser that uses `path-to-regexp` instead of `regexparam`
const pathToRegexpParser: Parser = (route: string) => {
const pathToRegexpParser: Parser = (route: string | RegExp) => {
const keys: Key[] = [];
const pattern = pathToRegexp(route, keys);

Expand Down
5 changes: 4 additions & 1 deletion packages/wouter/types/router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import {
BaseSearchHook,
} from "./location-hook.js";

export type Parser = (route: Path | RegExp, loose?: boolean) => { pattern: RegExp; keys: string[] };
export type Parser = (
route: Path | RegExp,
loose?: boolean
) => { pattern: RegExp; keys: string[] };

export type HrefsFormatter = (href: string, router: RouterObject) => string;

Expand Down

0 comments on commit 3e38ab6

Please sign in to comment.