Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update type definition #97

Merged
merged 3 commits into from
Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type Path = string;
export type PushCallback = (to: Path, replace?: boolean) => void;

export type LocationTuple = [Path, PushCallback];
export type LocationHook = () => LocationTuple;
export type LocationHook = (props?: Pick<RouterProps, 'basepath'>) => LocationTuple;

export interface DefaultParams {
[paramName: string]: string;
Expand Down Expand Up @@ -62,6 +62,7 @@ export const Switch: FunctionComponent<SwitchProps>;

export interface RouterProps {
hook: LocationHook;
basepath: Path;
matcher: MatcherFn;
}
export const Router: FunctionComponent<
Expand All @@ -74,4 +75,4 @@ export function useRouter(): RouterProps;

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

export function useLocation(): LocationTuple;
export function useLocation(props?: Pick<RouterProps, 'basepath'>): LocationTuple;
5 changes: 3 additions & 2 deletions types/preact/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type Path = string;
export type PushCallback = (to: Path, replace?: boolean) => void;

export type LocationTuple = [Path, PushCallback];
export type LocationHook = () => LocationTuple;
export type LocationHook = (props?: Pick<RouterProps, 'basepath'>) => LocationTuple;

export interface DefaultParams {
[paramName: string]: string;
Expand Down Expand Up @@ -57,6 +57,7 @@ export const Switch: FunctionComponent<SwitchProps>;

export interface RouterProps {
hook: LocationHook;
basepath: Path;
matcher: MatcherFn;
}
export const Router: FunctionComponent<
Expand All @@ -69,4 +70,4 @@ export function useRouter(): RouterProps;

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

export function useLocation(): LocationTuple;
export function useLocation(props?: Pick<RouterProps, 'basepath'>): LocationTuple;
18 changes: 18 additions & 0 deletions types/preact/type-specs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ const invalidParamsWithGeneric: Params<{ id: number }> = { id: 13 }; // $ExpectE
Hello, we have <Header /> and some {1337} numbers here.
</Router>;

<Router basepath="/app">
Hello World!
</Router>;

/*
* Hooks API
*/
Expand All @@ -147,3 +151,17 @@ if (params) {
} else {
params; // $ExpectType null
}

const [] = useLocation({base: '/app'}); // $ExpectError
const [, setLoc] = useLocation({basepath: '/base'});

setLoc('/app');

const [, parameters] = useRoute<{ id: string}>('/app/users/:id');

if (parameters) {
parameters.id; // $ExpectType string
parameters.age; // $ExpectError
} else {
parameters; // $ExpectType null
}
18 changes: 18 additions & 0 deletions types/type-specs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ const invalidParamsWithGeneric: Params<{ id: number }> = { id: 13 }; // $ExpectE
Hello, we have <Header /> and some {1337} numbers here.
</Router>;

<Router basepath="/app">
Hello World!
</Router>;

/*
* Hooks API
*/
Expand All @@ -162,3 +166,17 @@ if (params) {
} else {
params; // $ExpectType null
}

const [] = useLocation({base: '/app'}); // $ExpectError
const [, setLoc] = useLocation({basepath: '/base'});

setLoc('/app');

const [, parameters] = useRoute<{ id: string}>('/app/users/:id');

if (parameters) {
parameters.id; // $ExpectType string
parameters.age; // $ExpectError
} else {
parameters; // $ExpectType null
}