扩展不正确的Typescript类定义 [英] Extend an incorrect Typescript class definition

查看:158
本文介绍了扩展不正确的Typescript类定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中正在使用NPM包下一个路由.默认导出是一个具有如下类型定义的类:

I'm using the NPM package next-routes in my project. The default export is a class which has a type definition like so:

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;
}

完整文件可在软件包这里.

此类定义缺少包的默认导出(称为findAndGetUrls)中公开的方法之一.我该如何用自己的类型扩展类定义?我考虑过创建自己的类,该类实现NextRoutes,但定义缺少的方法定义,如下所示:

This class definition is missing one of the methods that is exposed in the package's default export called findAndGetUrls. How do I extend the class definition with my own type for this? I thought about creating my own class that implements NextRoutes but defines the missing method definition like so:

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文件:

My new attempt is to create a typings/next-routes.d.ts file in my project with the following:

import NextRoutes from 'next-routes';

declare module 'next-routes' {
  class Routes extends NextRoutes {
    findAndGetUrls(
      nameOrUrl: string,
      params: {
        [key: string]: string;
      },
    ): void;
  }

  export = Routes;
}

这使我的代码对findAndGetUrls的使用感到满意,但是现在它抱怨没有其他方法存在,因此无法正确扩展类型.例如Property 'add' does not exist on type 'Routes'.

This makes my code happy about the usage of findAndGetUrls, but now it complains that none of the other methods exist so it's not extending the types correctly. e.g. Property 'add' does not exist on type 'Routes'.

推荐答案

我已经玩了一段时间,Typescript不允许您重新定义类.但是,如果您将默认值重新导出为 interface 而不是类,这似乎可行:

I've been playing around with it for a while, Typescript won't allow you to redefine the class. But, it seems to work if you re-export the default as an interface instead of a 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合并.

You still have to name it Routes so that Typescript knows to merge it with the export default class Routes implements Registry from the next-routes package.

我仅在最新的Typescript版本(当前为3.5.2)上尝试过此操作,因此,如果您使用的是旧版本,则可以使用YMMV.

I've only tried this on the latest Typescript version (3.5.2 currently) so YMMV if you're using an older version.

这篇关于扩展不正确的Typescript类定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆