Skip to content

Commit

Permalink
Preact types: use shared types.
Browse files Browse the repository at this point in the history
  • Loading branch information
molefrog committed Jul 19, 2020
1 parent b727d7e commit 70c0330
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 68 deletions.
85 changes: 19 additions & 66 deletions types/preact/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// Type defitions for wouter are generously provided by:
// * Alexander Tolkunov <https://github.com/StrayFromThePath>
// * Maksim Karelov <https://github.com/Ty3uK>
// tslint:disable:no-unnecessary-generics

import {
JSX,
Expand All @@ -10,66 +8,19 @@ import {
VNode,
} from "preact";

/*
* Foundation: useLocation and paths
*/

export type Path = string;

// the base useLocation hook type. Any custom hook (including the
// default one) should inherit from it.
export type BaseLocationHook = (
...args: any[]
) => [Path, (path: Path, ...args: any[]) => any];

/*
* Utility types that operate on hook
*/

// Returns the type of the location tuple of the given hook.
export type HookReturnValue<H extends BaseLocationHook> = ReturnType<H>;

// Returns the type of the navigation options that hook's push function accepts.
export type HookNavigationOptions<H extends BaseLocationHook> = HookReturnValue<
H
>[1] extends (path: Path, options: infer R, ...rest: any[]) => any
? R extends { [k: string]: any }
? R
: {}
: {};

/*
* Default `useLocation`
*/

// The type of the default `useLocation` hook that wouter uses.
// It operates on current URL using History API, supports base path and can
// navigate with `pushState` or `replaceState`.
export type LocationHook = (options?: {
base?: Path;
}) => [Path, (to: Path, options?: { replace?: boolean }) => void];

export type LocationTuple = HookReturnValue<LocationHook>;

/*
* Match and params
*/

export interface DefaultParams {
[paramName: string]: string;
}
export type Params<T extends DefaultParams = DefaultParams> = T;
import {
Path,
BaseLocationHook,
HookReturnValue,
HookNavigationOptions,
LocationHook,
} from "./use-location";

export type MatchWithParams<T extends DefaultParams = DefaultParams> = [
true,
Params<T>
];
export type NoMatch = [false, null];
export type Match<T extends DefaultParams = DefaultParams> =
| MatchWithParams<T>
| NoMatch;
import { DefaultParams, Params, Match, MatcherFn } from "./matcher";

export type MatcherFn = (pattern: Path, path: Path) => Match;
// re-export types from these modules
export * from "./matcher";
export * from "./use-location";

/*
* Components: <Route />
Expand All @@ -85,7 +36,7 @@ export interface RouteProps<T extends DefaultParams = DefaultParams> {
component?: ComponentType<RouteComponentProps<T>>;
}
export function Route<T extends DefaultParams = DefaultParams>(
props: RouteProps<T> // tslint:disable-line:no-unnecessary-generics
props: RouteProps<T>
): VNode<any> | null;

/*
Expand All @@ -111,11 +62,11 @@ export type RedirectProps<
};

export function Link<H extends BaseLocationHook = LocationHook>(
props: LinkProps<H> // tslint:disable-line:no-unnecessary-generics
props: LinkProps<H>
): VNode<any> | null;

export function Redirect<H extends BaseLocationHook = LocationHook>(
props: RedirectProps<H> // tslint:disable-line:no-unnecessary-generics
props: RedirectProps<H>
): VNode<any> | null;

/*
Expand Down Expand Up @@ -150,8 +101,10 @@ export function useRouter(): RouterProps;

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

export function useLocation<
H extends BaseLocationHook = LocationHook
>(): HookReturnValue<H>; // tslint:disable-line:no-unnecessary-generics
>(): HookReturnValue<H>;

// tslint:enable:no-unnecessary-generics
6 changes: 4 additions & 2 deletions types/preact/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
"jsxFactory": "h",
"noEmit": true,
"strict": true,
"baseUrl": ".",
"paths": { "wouter/preact": ["."] }
"baseUrl": "../../preact",
"paths": {
"wouter/preact": ["."]
}
},
"files": ["./type-specs.tsx"]
}

0 comments on commit 70c0330

Please sign in to comment.