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 1 commit
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: string;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it's not really critical, but I would suggest using a Path type here (which is just an alias to string, but helps to keep the code more generic I guess). Basically Path represents something that can describe a location.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea!! Thx! i will fix it

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: string;
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
}
19 changes: 19 additions & 0 deletions types/type-specs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,15 @@ 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
*/
const [location, setLocation] = useLocation();

location; // $ExpectType string

setLocation(); // $ExpectError
Expand All @@ -162,3 +167,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
}