Skip to content

Commit 427aed0

Browse files
committed
Update Preact types with proper useLocation argument types.
1 parent 652d78c commit 427aed0

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

types/preact/index.d.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ export type Path = string;
1414
export type PushCallback = (to: Path, replace?: boolean) => void;
1515

1616
export type LocationTuple = [Path, PushCallback];
17-
export type LocationHook = (props?: Pick<RouterProps, 'base'>) => LocationTuple;
17+
export interface LocationHookOptions {
18+
base?: Path;
19+
}
20+
export type LocationHook = (options?: LocationHookOptions) => LocationTuple;
1821

1922
export interface DefaultParams {
2023
[paramName: string]: string;
@@ -25,9 +28,14 @@ export interface RouteComponentProps<T extends DefaultParams = DefaultParams> {
2528
params: T;
2629
}
2730

28-
export type MatchWithParams<T extends DefaultParams = DefaultParams> = [true, Params<T>];
31+
export type MatchWithParams<T extends DefaultParams = DefaultParams> = [
32+
true,
33+
Params<T>
34+
];
2935
export type NoMatch = [false, null];
30-
export type Match<T extends DefaultParams = DefaultParams> = MatchWithParams<T> | NoMatch;
36+
export type Match<T extends DefaultParams = DefaultParams> =
37+
| MatchWithParams<T>
38+
| NoMatch;
3139

3240
export type MatcherFn = (pattern: Path, path: Path) => Match;
3341

@@ -36,7 +44,9 @@ export interface RouteProps<T extends DefaultParams = DefaultParams> {
3644
path: Path;
3745
component?: ComponentType<RouteComponentProps<T>>;
3846
}
39-
export function Route<T extends DefaultParams = DefaultParams>(props: RouteProps<T>): VNode<any> | null; // tslint:disable-line:no-unnecessary-generics
47+
export function Route<T extends DefaultParams = DefaultParams>(
48+
props: RouteProps<T> // tslint:disable-line:no-unnecessary-generics
49+
): VNode<any> | null;
4050

4151
export type NavigationalProps =
4252
| { to: Path; href?: never }
@@ -60,14 +70,14 @@ export interface RouterProps {
6070
base: Path;
6171
matcher: MatcherFn;
6272
}
63-
export const Router: FunctionComponent<
64-
Partial<RouterProps> & {
65-
children: ComponentChildren;
66-
}
67-
>;
73+
export const Router: FunctionComponent<Partial<RouterProps> & {
74+
children: ComponentChildren;
75+
}>;
6876

6977
export function useRouter(): RouterProps;
7078

71-
export function useRoute<T extends DefaultParams = DefaultParams>(pattern: Path): Match<T>; // tslint:disable-line:no-unnecessary-generics
79+
export function useRoute<T extends DefaultParams = DefaultParams>(
80+
pattern: Path
81+
): Match<T>; // tslint:disable-line:no-unnecessary-generics
7282

73-
export function useLocation(props?: Pick<RouterProps, 'base'>): LocationTuple;
83+
export function useLocation(): LocationTuple;

types/preact/type-specs.tsx

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import {
88
Router,
99
Switch,
1010
useLocation,
11-
useRoute,
11+
useRoute
1212
} from "wouter/preact";
1313

1414
const Header: FunctionComponent = () => <div />;
1515
const Profile = ({ params }: RouteComponentProps<{ id: string }>) => (
16-
<div>User id: {params.id}</div>
16+
<div>User id: {params.id}</div>
1717
);
1818

1919
/*
@@ -23,7 +23,7 @@ const someParams: Params = { foo: "bar" };
2323
const someParamsWithGeneric: Params<{ foo: string }> = { foo: "bar" };
2424

2525
// error: params should follow generic type
26-
const paramsDontMatchGeneric: Params<{ foo: string }> = { baz: "bar" }; // $ExpectError
26+
const paramsDontMatchGeneric: Params<{ foo: string }> = { baz: "bar" }; // $ExpectError
2727

2828
// error: values are strings!
2929
const invalidParams: Params = { id: 13 }; // $ExpectError
@@ -57,9 +57,7 @@ const invalidParamsWithGeneric: Params<{ id: number }> = { id: 13 }; // $ExpectE
5757
{(params: Params): React.ReactNode => `User id: ${params.id}`}
5858
</Route>;
5959

60-
<Route<{ id: string }> path="/users/:id">
61-
{({ id }) => `User id: ${id}`}
62-
</Route>;
60+
<Route<{ id: string }> path="/users/:id">{({ id }) => `User id: ${id}`}</Route>;
6361

6462
<Route<{ id: string }> path="/users/:id">
6563
{({ age }) => `User age: ${age}`} // $ExpectError
@@ -124,9 +122,7 @@ const invalidParamsWithGeneric: Params<{ id: number }> = { id: 13 }; // $ExpectE
124122
Hello, we have <Header /> and some {1337} numbers here.
125123
</Router>;
126124

127-
<Router base="/app">
128-
Hello World!
129-
</Router>;
125+
<Router base="/app">Hello World!</Router>;
130126

131127
/*
132128
* Hooks API
@@ -138,6 +134,8 @@ setLocation(); // $ExpectError
138134
setLocation("/app");
139135
setLocation("/app", true);
140136

137+
useLocation({ base: "/app" }); // $ExpectError
138+
141139
useRoute(Symbol()); // $ExpectError
142140
useRoute(); // $ExpectError
143141
useRoute("/");
@@ -152,12 +150,7 @@ if (params) {
152150
params; // $ExpectType null
153151
}
154152

155-
const [] = useLocation({basename: '/app'}); // $ExpectError
156-
const [, setLoc] = useLocation({base: '/base'});
157-
158-
setLoc('/app');
159-
160-
const [, parameters] = useRoute<{ id: string}>('/app/users/:id');
153+
const [, parameters] = useRoute<{ id: string }>("/app/users/:id");
161154

162155
if (parameters) {
163156
parameters.id; // $ExpectType string

0 commit comments

Comments
 (0)