From 268fe2d0fbc364b02183e3264304dd505ef0c4bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Can=20Eri=C5=9F?= Date: Wed, 15 May 2019 00:51:43 +0300 Subject: [PATCH] [hookrouter] Add type definitions for react hookrouter. (#35443) * Add tests. * Add types. * Add package.json with csstype dependency. * Add tslint.json and tsconfig.json. --- types/hookrouter/hookrouter-tests.ts | 81 ++++++++++++++++++++++++++++ types/hookrouter/index.d.ts | 56 +++++++++++++++++++ types/hookrouter/package.json | 6 +++ types/hookrouter/tsconfig.json | 23 ++++++++ types/hookrouter/tslint.json | 1 + 5 files changed, 167 insertions(+) create mode 100644 types/hookrouter/hookrouter-tests.ts create mode 100644 types/hookrouter/index.d.ts create mode 100644 types/hookrouter/package.json create mode 100644 types/hookrouter/tsconfig.json create mode 100644 types/hookrouter/tslint.json diff --git a/types/hookrouter/hookrouter-tests.ts b/types/hookrouter/hookrouter-tests.ts new file mode 100644 index 0000000000..534f374932 --- /dev/null +++ b/types/hookrouter/hookrouter-tests.ts @@ -0,0 +1,81 @@ +import { + A, + setLinkProps, + useControlledInterceptor, + interceptRoute, + get, + remove, + navigate, + setPath, + getPath, + getTitle, + useRedirect, + useRoutes, + usePath, + getWorkingPath, + getBasepath, + resolvePath, + prepareRoute, + useQueryParams +} from 'hookrouter'; +import * as React from 'react'; + +// $ExpectType AProps +setLinkProps({ href: '/route' }); + +// $ExpectError +setLinkProps({ onClick: () => null }); + +// $ExpectType ReactHTMLElement +A({ href: '/route' }); + +// $ExpectType [InterceptedPath, () => void, () => void, () => void] +useControlledInterceptor(); + +// $ExpectType string[] +interceptRoute('/route1', '/route2'); + +// $ExpectType RouteObject | null +get(2); + +// $ExpectType void +remove(2); + +// $ExpectType [QueryParams, (inObj: QueryParams, replace?: boolean | undefined) => void] +useQueryParams(); + +// $ExpectType void +useRedirect('/route1', '/route2'); + +// $ExpectError +navigate(); + +// $ExpectError +navigate(1); + +// $ExpectType string +getBasepath(); + +// $ExpectType string +resolvePath('path'); + +// $ExpectType [RegExp, string[]] +prepareRoute('/route'); + +// $ExpectType void +setPath('/route'); + +// $ExpectType string +usePath(); + +// $ExpectType string +usePath(true, true); + +// $ExpectType string +getWorkingPath('id'); + +// $ExpectType ReactNode +useRoutes({ '/route:id': ({ id }) => React.createElement('div', { children: 'Route Component' }) }); + +// $ExpectType string +getTitle(); diff --git a/types/hookrouter/index.d.ts b/types/hookrouter/index.d.ts new file mode 100644 index 0000000000..9076efa8df --- /dev/null +++ b/types/hookrouter/index.d.ts @@ -0,0 +1,56 @@ +// Type definitions for hookrouter 2.2 +// Project: https://github.com/Paratron/hookrouter +// Definitions by: Mete Can Eris +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.4 +import * as React from 'react'; +export namespace HookRouter { + type InterceptedPath = string | null; + interface AProps extends React.AnchorHTMLAttributes { href: string; } + interface QueryParams { [key: string]: any; } + interface RouteObject { [key: string]: (params: QueryParams) => any; } +} +export function setLinkProps(props: HookRouter.AProps): HookRouter.AProps; +export function A(props: HookRouter.AProps): React.ReactHTMLElement; +export function confirmNavigation(): void; +export function resetPath(): void; +export function stopInterception(): void; +export function useControlledInterceptor(): [ + HookRouter.InterceptedPath, + typeof confirmNavigation, + typeof resetPath, + typeof stopInterception +]; +export function interceptRoute(previousRoute: string, nextRoute: string): string[]; +export function get(componentId: number): HookRouter.RouteObject | null; +export function remove(componentId: number): void; +export function useInterceptor(handlerFn: (...args: any[]) => any): () => typeof remove; +export function setQueryParams(inObj: HookRouter.QueryParams, replace?: boolean): void; +export function getQueryParams(): HookRouter.QueryParams; +export function queryStringToObject(inStr: string): HookRouter.QueryParams; +export function objectToQueryString(inObj: HookRouter.QueryParams): string; +export function useQueryParams(): [HookRouter.QueryParams, typeof setQueryParams]; +export function useRedirect( + fromURL: string, + toURL: string, + queryParams?: HookRouter.QueryParams | null, + replace?: boolean +): void; +export function setBasepath(inBasepath: string): void; +export function getBasepath(): string; +export function resolvePath(inPath: string): string; +export function prepareRoute(inRoute: string): [RegExp, string[]]; +export function navigate( + url: string, + replace: true, + queryParams?: HookRouter.QueryParams | null, + replaceQueryParams?: boolean +): void; +export function setPath(inPath: string): void; +export function getPath(): string; +export function usePath(active?: boolean, withBasePath?: boolean): string; +export function updatePathHooks(): void; +export function getWorkingPath(parentRouterId: string): string; +export function useRoutes(routeObj: HookRouter.RouteObject): React.ReactNode; +export function useTitle(inString: string): void; +export function getTitle(): string; diff --git a/types/hookrouter/package.json b/types/hookrouter/package.json new file mode 100644 index 0000000000..4d9ec4a338 --- /dev/null +++ b/types/hookrouter/package.json @@ -0,0 +1,6 @@ +{ + "private": true, + "dependencies": { + "csstype": "^2.2.0" + } +} diff --git a/types/hookrouter/tsconfig.json b/types/hookrouter/tsconfig.json new file mode 100644 index 0000000000..318d89ed37 --- /dev/null +++ b/types/hookrouter/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "hookrouter-tests.ts" + ] +} diff --git a/types/hookrouter/tslint.json b/types/hookrouter/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/hookrouter/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }