[hookrouter] Add type definitions for react hookrouter. (#35443)

* Add tests.

* Add types.

* Add package.json with csstype dependency.

* Add tslint.json and tsconfig.json.
This commit is contained in:
Can Eriş
2019-05-14 14:51:43 -07:00
committed by Nathan Shively-Sanders
parent c46c58352f
commit 268fe2d0fb
5 changed files with 167 additions and 0 deletions
+81
View File
@@ -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<HTMLAnchorElement>
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();
+56
View File
@@ -0,0 +1,56 @@
// Type definitions for hookrouter 2.2
// Project: https://github.com/Paratron/hookrouter
// Definitions by: Mete Can Eris <https://github.com/mcaneris>
// 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<HTMLAnchorElement> { 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<HTMLAnchorElement>;
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;
+6
View File
@@ -0,0 +1,6 @@
{
"private": true,
"dependencies": {
"csstype": "^2.2.0"
}
}
+23
View File
@@ -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"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }