From 7593e911bf0b0ed936aee5a371ae7066d4fb54ae Mon Sep 17 00:00:00 2001 From: gillchristian Date: Fri, 14 Sep 2018 11:46:58 +0200 Subject: [PATCH] Update definitions for react-router/v3 --- types/react-router/v3/index.d.ts | 1 + types/react-router/v3/lib/PatternUtils.d.ts | 9 +++++++++ types/react-router/v3/react-router-tests.tsx | 11 +++++++++++ 3 files changed, 21 insertions(+) diff --git a/types/react-router/v3/index.d.ts b/types/react-router/v3/index.d.ts index ee4e01ba40..c8c59978bc 100644 --- a/types/react-router/v3/index.d.ts +++ b/types/react-router/v3/index.d.ts @@ -10,6 +10,7 @@ // Karol Janyst // Dovydas Navickas // Ross Allen +// Christian Gill // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.8 diff --git a/types/react-router/v3/lib/PatternUtils.d.ts b/types/react-router/v3/lib/PatternUtils.d.ts index 2c32b2a286..ebcea83292 100644 --- a/types/react-router/v3/lib/PatternUtils.d.ts +++ b/types/react-router/v3/lib/PatternUtils.d.ts @@ -1,3 +1,12 @@ import { RoutePattern } from "react-router"; export function formatPattern(pattern: RoutePattern, params: any): string; + +export function matchPattern( + pattern: string, + pathname: string +): { + remainingPathname: string; + paramNames: string[]; + paramValues: string[]; +} | null; diff --git a/types/react-router/v3/react-router-tests.tsx b/types/react-router/v3/react-router-tests.tsx index 5ff331a78a..a41ac79373 100644 --- a/types/react-router/v3/react-router-tests.tsx +++ b/types/react-router/v3/react-router-tests.tsx @@ -23,6 +23,7 @@ import { RouteComponentProps, WithRouterProps } from "react-router"; +import { matchPattern } from 'react-router/lib/PatternUtils'; import { createHistory, History } from "history"; const routerHistory = useRouterHistory(createHistory)({ basename: "/test" }); @@ -181,3 +182,13 @@ ReactDOM.render(( > ), document.body); + +const matchedPattern = matchPattern("/foo", "/foo/bar"); + +if (matchedPattern) { + matchedPattern.remainingPathname === "/bar"; + matchedPattern.paramNames.forEach(name => {}); + matchedPattern.paramValues.forEach(value => {}); +} + +matchPattern("/foo", "/baz") === null;