From 8bccf6cb2cae6f721107b25f0ce511a24b04b51e Mon Sep 17 00:00:00 2001 From: kingdaro Date: Mon, 4 Jun 2018 22:25:06 -0400 Subject: [PATCH] add reach router types --- types/reach__router/index.d.ts | 142 ++++++++++++++++++++ types/reach__router/reach__router-tests.tsx | 26 ++++ types/reach__router/tsconfig.json | 20 +++ types/reach__router/tslint.json | 1 + 4 files changed, 189 insertions(+) create mode 100644 types/reach__router/index.d.ts create mode 100644 types/reach__router/reach__router-tests.tsx create mode 100644 types/reach__router/tsconfig.json create mode 100644 types/reach__router/tslint.json diff --git a/types/reach__router/index.d.ts b/types/reach__router/index.d.ts new file mode 100644 index 0000000000..869329ef23 --- /dev/null +++ b/types/reach__router/index.d.ts @@ -0,0 +1,142 @@ +// Type definitions for @reach/router 1.0 +// Project: https://github.com/reach/router +// Definitions by: Kingdaro +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 + +import * as React from "react"; + +export type WindowLocation = Window["location"]; + +export interface History { + readonly location: string; + readonly transitioning: boolean; + listen: (listener: HistoryListener) => HistoryUnsubscribe; + navigate: NavigateFn; +} + +export type HistoryListener = () => void; +export type HistoryUnsubscribe = () => void; + +export class Router extends React.Component {} + +export interface RouterProps { + basepath?: string; + primary?: boolean; + location?: WindowLocation; +} + +export type RouteComponentProps = Partial & { + path?: string; + default?: boolean; + location?: WindowLocation; + navigate?: NavigateFn; + uri?: string; +}; + +export type Omit = Pick>; + +export type AnchorProps = Omit< + React.DetailedHTMLProps< + React.AnchorHTMLAttributes, + HTMLAnchorElement + >, + "href" // remove href, as it's ignored by the router +>; + +export interface LinkProps extends AnchorProps { + to?: string; + replace?: boolean; + getProps?: (props: LinkGetProps) => {}; + state?: TState; +} + +export interface LinkGetProps { + isCurrent: boolean; + isPartiallyCurrent: boolean; + href: string; + location: WindowLocation; +} + +export class Link extends React.Component> {} + +export interface RedirectProps { + from?: string; + to: string; + noThrow?: boolean; +} + +export class Redirect extends React.Component {} + +export interface MatchProps { + path: string; + children: MatchRenderFn; +} + +export type MatchRenderFn = ( + props: MatchRenderProps, +) => React.ReactNode; + +export interface MatchRenderProps { + match: null | { uri: string; path: string } & TParams; + location: WindowLocation; + navigate: NavigateFn; +} + +export class Match extends React.Component> {} + +export type NavigateFn = (to: string, options?: NavigateOptions<{}>) => void; + +export interface NavigateOptions { + state?: TState; + replace?: boolean; +} + +export class Location extends React.Component {} + +export interface LocationProviderProps { + history: History; + children?: React.ReactNode | LocationProviderRenderFn; +} + +export type LocationProviderRenderFn = ( + context: LocationContext, +) => React.ReactNode; + +export interface LocationContext { + location: WindowLocation; + navigate: NavigateFn; +} + +export class LocationProvider extends React.Component {} + +export interface ServerLocationProps { + url: string; +} + +export class ServerLocation extends React.Component {} + +export const navigate: NavigateFn; + +export interface HistorySource { + readonly location: WindowLocation; + addEventListener(name: string, listener: (event: Event) => void): void; + removeEventListener(name: string, listener: (event: Event) => void): void; + history: { + readonly state: any; + pushState(state: any, title: string, uri: string): void; + replaceState(state: any, title: string, uri: string): void; + }; +} + +export function createHistory(source: HistorySource): History; + +export function createMemorySource(initialPath: string): HistorySource; + +export interface RedirectRequest { + uri: string; +} + +export function isRedirect(error: any): error is RedirectRequest; + +export function redirectTo(uri: string): void; diff --git a/types/reach__router/reach__router-tests.tsx b/types/reach__router/reach__router-tests.tsx new file mode 100644 index 0000000000..f20b11483f --- /dev/null +++ b/types/reach__router/reach__router-tests.tsx @@ -0,0 +1,26 @@ +import { Link, RouteComponentProps, Router } from "@reach/router"; +import * as React from "react"; +import { render } from "react-dom"; + +interface DashParams { + id: string; +} + +const Home = (props: RouteComponentProps) =>
Home
; + +const Dash = (props: RouteComponentProps) => ( +
Dash for item ${props.id}
+); + +const NotFound = (props: RouteComponentProps) =>
Route not found
; + +render( + + + + + + + , + document.getElementById("app-root"), +); diff --git a/types/reach__router/tsconfig.json b/types/reach__router/tsconfig.json new file mode 100644 index 0000000000..ba13d27ded --- /dev/null +++ b/types/reach__router/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["dom", "es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "jsx": "react", + "paths": { + "@reach/router": ["reach__router"] + } + }, + "files": ["index.d.ts", "reach__router-tests.tsx"] +} diff --git a/types/reach__router/tslint.json b/types/reach__router/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/reach__router/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }