add reach router types

This commit is contained in:
kingdaro
2018-06-04 22:25:06 -04:00
parent b21fbc2d15
commit 8bccf6cb2c
4 changed files with 189 additions and 0 deletions
+142
View File
@@ -0,0 +1,142 @@
// Type definitions for @reach/router 1.0
// Project: https://github.com/reach/router
// Definitions by: Kingdaro <https://github.com/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<RouterProps> {}
export interface RouterProps {
basepath?: string;
primary?: boolean;
location?: WindowLocation;
}
export type RouteComponentProps<TParams = {}> = Partial<TParams> & {
path?: string;
default?: boolean;
location?: WindowLocation;
navigate?: NavigateFn;
uri?: string;
};
export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
export type AnchorProps = Omit<
React.DetailedHTMLProps<
React.AnchorHTMLAttributes<HTMLAnchorElement>,
HTMLAnchorElement
>,
"href" // remove href, as it's ignored by the router
>;
export interface LinkProps<TState> 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<TState> extends React.Component<LinkProps<TState>> {}
export interface RedirectProps {
from?: string;
to: string;
noThrow?: boolean;
}
export class Redirect extends React.Component<RedirectProps> {}
export interface MatchProps<TParams> {
path: string;
children: MatchRenderFn<TParams>;
}
export type MatchRenderFn<TParams> = (
props: MatchRenderProps<TParams>,
) => React.ReactNode;
export interface MatchRenderProps<TParams> {
match: null | { uri: string; path: string } & TParams;
location: WindowLocation;
navigate: NavigateFn;
}
export class Match<TParams> extends React.Component<MatchProps<TParams>> {}
export type NavigateFn = (to: string, options?: NavigateOptions<{}>) => void;
export interface NavigateOptions<TState> {
state?: TState;
replace?: boolean;
}
export class Location extends React.Component<LocationContext> {}
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<LocationProviderProps> {}
export interface ServerLocationProps {
url: string;
}
export class ServerLocation extends React.Component<ServerLocationProps> {}
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;
@@ -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) => <div>Home</div>;
const Dash = (props: RouteComponentProps<DashParams>) => (
<div>Dash for item ${props.id}</div>
);
const NotFound = (props: RouteComponentProps) => <div>Route not found</div>;
render(
<Router>
<Home path="/" />
<Dash path="/default/:id" />
<NotFound default />
<Link to="/somepath" rel="noopener noreferrer" target="_blank" />
</Router>,
document.getElementById("app-root"),
);
+20
View File
@@ -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"]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }