Skip to content

Commit

Permalink
Fix typescript definitions (#200)
Browse files Browse the repository at this point in the history
* Fix typescript definitions

Fixes errors present in strict mode

Adds getCurrentUrl and route override definitions

Adds a RoutableProps interface consumers can extend to implement a
routable component

Fixes the types of RouterProps, Route, Link (closes #133)

Re-enables the typings file in package.json

* add generic over Route, RouteProps
  • Loading branch information
Alexendoo authored and developit committed Jun 13, 2017
1 parent 7e96ec4 commit 3954489
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"dist",
"match.js"
],
"x-typings": "./dist/index.d.ts",
"typings": "./dist/index.d.ts",
"keywords": [
"preact",
"router"
Expand Down
38 changes: 22 additions & 16 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import * as preact from 'preact';

export function route(url: string, replace?: boolean): boolean;
export function route(options: { url: string; replace?: boolean }): boolean;

export function getCurrentUrl(): string;

export interface CustomHistory {
getCurrentLocation?: () => string;
Expand All @@ -8,35 +13,36 @@ export interface CustomHistory {
replace?: (url: string) => void;
}

export interface RouterProps extends JSX.HTMLAttributes, preact.ComponentProps {
history?: CustomHistory;
export interface RoutableProps {
path?: string;
default?: boolean;
}

export interface RouterProps extends RoutableProps {
history?: CustomHistory;
static?: boolean;
url?: string;
}

export class Router extends preact.Component<RouterProps, {}> {
canRoute(url: string): boolean;
getMatchingChildren(children: preact.VNode[], url: string, invoke: boolean): preact.VNode[];
getMatchingChildren(
children: preact.VNode[],
url: string,
invoke: boolean
): preact.VNode[];
routeTo(url: string): boolean;
render(props: RouterProps, {}): preact.VNode;
}

export interface RouteArgs<PropsType, StateType> {
component: preact.Component<PropsType, StateType>;
path: string;
matches?: boolean;
url?: string;
export interface RouteProps<C> extends RoutableProps {
component: preact.AnyComponent<C, any>;
}

export function Route<PropsType, StateType>({component, url, matches}: RouteArgs<PropsType, StateType>): preact.VNode;
export function Route<C>(
props: RouteProps<C> & { [P in keyof C]: C[P] }
): preact.VNode;

export function Link(props: any): preact.VNode;

export namespace Router {
var route: ((url: string, replace?: boolean) => boolean);
var Route: (({component, url, matches}) => preact.VNode);
var Link: ((props: any) => preact.VNode);
}
export function Link(props: JSX.HTMLAttributes): preact.VNode;

export default Router;

0 comments on commit 3954489

Please sign in to comment.