From 3954489baf9c3ea5cdb2dcfc9ca61b1690fffc6c Mon Sep 17 00:00:00 2001 From: Alex Macleod Date: Tue, 13 Jun 2017 21:48:03 +0100 Subject: [PATCH] Fix typescript definitions (#200) * 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 --- package.json | 2 +- src/index.d.ts | 38 ++++++++++++++++++++++---------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 1e6e1ea9..cdde9d92 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "dist", "match.js" ], - "x-typings": "./dist/index.d.ts", + "typings": "./dist/index.d.ts", "keywords": [ "preact", "router" diff --git a/src/index.d.ts b/src/index.d.ts index e007ad25..b5ce0f61 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -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; @@ -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 { 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 { - component: preact.Component; - path: string; - matches?: boolean; - url?: string; +export interface RouteProps extends RoutableProps { + component: preact.AnyComponent; } -export function Route({component, url, matches}: RouteArgs): preact.VNode; +export function Route( + props: RouteProps & { [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;