From fde76994561dd7151f8b002fd646690c176e85a3 Mon Sep 17 00:00:00 2001 From: Scott Jones Date: Thu, 19 Apr 2018 23:42:02 -0400 Subject: [PATCH] Fix up next/router types * add name to index.ts * update tests --- types/next/index.d.ts | 1 + types/next/router.d.ts | 8 +++-- types/next/test/next-router-tests.tsx | 46 +++++++++++++-------------- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/types/next/index.d.ts b/types/next/index.d.ts index 036e9f58ce..e0ec456724 100644 --- a/types/next/index.d.ts +++ b/types/next/index.d.ts @@ -2,6 +2,7 @@ // Project: https://github.com/zeit/next.js // Definitions by: Drew Hays // Brice BERNARD +// Scott Jones // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.6 diff --git a/types/next/router.d.ts b/types/next/router.d.ts index 181208518f..a5cfb42c90 100644 --- a/types/next/router.d.ts +++ b/types/next/router.d.ts @@ -9,7 +9,7 @@ export interface EventChangeOptions { } export type RouterCallback = () => void; -export interface SingletonRouter { +export interface RouterProps { readyCallbacks: RouterCallback[]; ready(cb: RouterCallback): void; @@ -53,8 +53,12 @@ export interface SingletonRouter { onRouteChangeError?(error: any, url: string): void; } +export interface SingletonRouter { + router: RouterProps; +} + export function withRouter( - Component: React.ComponentType, + Component: React.ComponentType, ): React.ComponentType; export const Singleton: SingletonRouter; diff --git a/types/next/test/next-router-tests.tsx b/types/next/test/next-router-tests.tsx index fe82d6f0b2..7957f5e8a2 100644 --- a/types/next/test/next-router-tests.tsx +++ b/types/next/test/next-router-tests.tsx @@ -2,10 +2,10 @@ import Router, * as r from "next/router"; import * as React from "react"; import * as qs from "querystring"; -Router.readyCallbacks.push(() => { +Router.router.readyCallbacks.push(() => { console.log("I'll get called when the router initializes."); }); -Router.ready(() => { +Router.router.ready(() => { console.log( "I'll get called immediately if the router initializes, or when it eventually does.", ); @@ -13,8 +13,8 @@ Router.ready(() => { // Access readonly properties of the router. -Object.keys(Router.components).forEach(key => { - const c = Router.components[key]; +Object.keys(Router.router.components).forEach(key => { + const c = Router.router.components[key]; c.err.isAnAny; return ; @@ -26,51 +26,51 @@ function split(routeLike: string) { }); } -if (Router.asPath) { - split(Router.asPath); - split(Router.asPath); +if (Router.router.asPath) { + split(Router.router.asPath); + split(Router.router.asPath); } -split(Router.pathname); +split(Router.router.pathname); -const query = `?${qs.stringify(Router.query)}`; +const query = `?${qs.stringify(Router.router.query)}`; // Assign some callback methods. -Router.onAppUpdated = (nextRoute: string) => console.log(nextRoute); -Router.onRouteChangeStart = (url: string) => +Router.router.onAppUpdated = (nextRoute: string) => console.log(nextRoute); +Router.router.onRouteChangeStart = (url: string) => console.log("Route is starting to change.", url); -Router.onBeforeHistoryChange = (as: string) => +Router.router.onBeforeHistoryChange = (as: string) => console.log("History hasn't changed yet.", as); -Router.onRouteChangeComplete = (url: string) => +Router.router.onRouteChangeComplete = (url: string) => console.log("Route chaneg is complete.", url); -Router.onRouteChangeError = (err: any, url: string) => +Router.router.onRouteChangeError = (err: any, url: string) => console.log("Route is starting to change.", url, err); // Call methods on the router itself. -Router.reload("/route").then(() => console.log("route was reloaded")); -Router.back(); +Router.router.reload("/route").then(() => console.log("route was reloaded")); +Router.router.back(); -Router.push("/route").then((success: boolean) => +Router.router.push("/route").then((success: boolean) => console.log("route push success: ", success), ); -Router.push("/route", "/asRoute").then((success: boolean) => +Router.router.push("/route", "/asRoute").then((success: boolean) => console.log("route push success: ", success), ); -Router.push("/route", "/asRoute", { shallow: false }).then((success: boolean) => +Router.router.push("/route", "/asRoute", { shallow: false }).then((success: boolean) => console.log("route push success: ", success), ); -Router.replace("/route").then((success: boolean) => +Router.router.replace("/route").then((success: boolean) => console.log("route replace success: ", success), ); -Router.replace("/route", "/asRoute").then((success: boolean) => +Router.router.replace("/route", "/asRoute").then((success: boolean) => console.log("route replace success: ", success), ); -Router.replace("/route", "/asRoute", { +Router.router.replace("/route", "/asRoute", { shallow: false, }).then((success: boolean) => console.log("route replace success: ", success)); -Router.prefetch("/route").then(Component => { +Router.router.prefetch("/route").then(Component => { const element = ; });