扩展不正确的 Typescript class 定义
Extend an incorrect Typescript class definition
我在我的项目中使用 NPM 包 next-routes。默认导出是一个 class 类型定义如下:
export default class Routes implements Registry {
getRequestHandler(app: Server, custom?: HTTPHandler): HTTPHandler;
add(name: string, pattern?: string, page?: string): this;
add(pattern: string, page: string): this;
add(options: { name: string; pattern?: string; page?: string }): this;
Link: ComponentType<LinkProps>;
Router: Router;
}
完整文件可以在包 here.
中找到
此 class 定义缺少包的默认导出中公开的方法之一,称为 findAndGetUrls
。我如何为此使用我自己的类型扩展 class 定义?我考虑过创建自己的 class 来实现 NextRoutes 但像这样定义缺少的方法定义:
import NextRoutes from 'next-routes';
class Routes implements NextRoutes {
findAndGetUrls(
nameOrUrl: string,
params: {
[key: string]: string;
},
): void;
}
但是这个错误:Function implementation is missing or not immediately following the declaration.
编辑
我的新尝试是在我的项目中创建一个 typings/next-routes.d.ts
文件,内容如下:
import NextRoutes from 'next-routes';
declare module 'next-routes' {
class Routes extends NextRoutes {
findAndGetUrls(
nameOrUrl: string,
params: {
[key: string]: string;
},
): void;
}
export = Routes;
}
这让我的代码对 findAndGetUrls 的使用很满意,但现在它抱怨 none 存在其他方法,因此它没有正确扩展类型。例如Property 'add' does not exist on type 'Routes'.
我已经研究了一段时间,Typescript 不允许您重新定义 class。但是,如果您将 re-export 默认设置为 接口 而不是 class:
,它似乎可以工作
custom-next-routes.d.ts:
import NextRoutes, { RouteParams } from "next-routes";
declare module "next-routes" {
export default interface Routes extends NextRoutes {
findAndGetUrls(nameOrUrl: string, params: RouteParams): void;
}
}
您仍然必须将其命名为 Routes
,以便 Typescript 知道将其与 next-routes
包中的 export default class Routes implements Registry
合并。
我只在最新的 Typescript 版本(目前为 3.5.2)上尝试过,所以如果您使用的是旧版本,请使用 YMMV。
我在我的项目中使用 NPM 包 next-routes。默认导出是一个 class 类型定义如下:
export default class Routes implements Registry {
getRequestHandler(app: Server, custom?: HTTPHandler): HTTPHandler;
add(name: string, pattern?: string, page?: string): this;
add(pattern: string, page: string): this;
add(options: { name: string; pattern?: string; page?: string }): this;
Link: ComponentType<LinkProps>;
Router: Router;
}
完整文件可以在包 here.
中找到此 class 定义缺少包的默认导出中公开的方法之一,称为 findAndGetUrls
。我如何为此使用我自己的类型扩展 class 定义?我考虑过创建自己的 class 来实现 NextRoutes 但像这样定义缺少的方法定义:
import NextRoutes from 'next-routes';
class Routes implements NextRoutes {
findAndGetUrls(
nameOrUrl: string,
params: {
[key: string]: string;
},
): void;
}
但是这个错误:Function implementation is missing or not immediately following the declaration.
编辑
我的新尝试是在我的项目中创建一个 typings/next-routes.d.ts
文件,内容如下:
import NextRoutes from 'next-routes';
declare module 'next-routes' {
class Routes extends NextRoutes {
findAndGetUrls(
nameOrUrl: string,
params: {
[key: string]: string;
},
): void;
}
export = Routes;
}
这让我的代码对 findAndGetUrls 的使用很满意,但现在它抱怨 none 存在其他方法,因此它没有正确扩展类型。例如Property 'add' does not exist on type 'Routes'.
我已经研究了一段时间,Typescript 不允许您重新定义 class。但是,如果您将 re-export 默认设置为 接口 而不是 class:
,它似乎可以工作custom-next-routes.d.ts:
import NextRoutes, { RouteParams } from "next-routes";
declare module "next-routes" {
export default interface Routes extends NextRoutes {
findAndGetUrls(nameOrUrl: string, params: RouteParams): void;
}
}
您仍然必须将其命名为 Routes
,以便 Typescript 知道将其与 next-routes
包中的 export default class Routes implements Registry
合并。
我只在最新的 Typescript 版本(目前为 3.5.2)上尝试过,所以如果您使用的是旧版本,请使用 YMMV。