typescript - 在不破坏现有合约的情况下向导出类型添加函数
问题描述
我想为导出的类型添加一个新函数,该类型已在 prod 中使用,因此不想重命名任何内容或引入重大更改。
import * as BunyanLogger from 'bunyan';
import init from './logger';
export type Logger = BunyanLogger;
我尝试使用接口而不是类型,但它需要我在BunyanLogger
.
我想Logger
用一种额外的方法来扩展类型,我尝试过如下所示的交集,但也没有用:
export type Logger = BunyanLogger & {
debugInPlace: (featureRole?:string, ...params: any[]) => void;
};
有没有办法向类型添加额外的方法,而Logger
不必重新实现或委托给BunyanLogger
类型中的方法?
解决方案
我能够通过导入整个bunyan
命名空间并将其实现为接口来解决此问题。
import Logger from 'bunyan';
import * as bunyanLogger from "bunyan";
export class ExtendedLogger extends Logger implements bunyanLogger {
constructor(options: Logger.LoggerOptions) {
super(options);
}
}
推荐阅读
- git - 如何合并外部更改到分支
- c++ - 为什么 std::future 从 std::packaged_task 和 std::async 返回不同?
- ios - 键盘出现时是否可以“向上推”整个应用程序?
- typescript - 将在 Typescript 中使用 --strictNullChecks 从 DB 或 API 接收异步数据的变量的类型?
- python - 熊猫根据来自另一个数据帧的组创建多个数据帧
- flutter - 在散点图中改变半径和颜色颤动
- reactjs - 组件未在 React useState 中更新
- dropdown - ACA 应用程序不将 repo 约束属性显示为下拉列表
- c++ - C++运算符++重载是如何工作的
- php - 将站点从一台主机移动到另一台主机后出现致命错误