如何重载打字稿的“path.normalize”定义?

时间:2021-07-16 16:01:42

标签: typescript

我正在将一个流项目转换为 TS,我们拥有的一件事是一堆不透明的类型,例如fileNamerelativePathabsolutePath。在流程中,我们重载 path 以使用这些类型:

declare module 'path' {
  declare function normalize(path: absolutePath): absolutePath;
  declare function normalize(path: string): relativePath;

我正在尝试在 TS 中做同样的事情,但无法弄清楚如何 a) 替换现有的 path 定义(我很好地复制了我们执行此操作的 3 个文件)或 b ) 将我的定义合并到现有的定义中。无论我尝试什么,它都使用 @types/node 中的那些(尽管 VS 代码显示 TS 看到这两个文件,但它总是选择那个)。 我想我可以删除它并复制所有内容,但我宁愿不用于维护。我什至无法复制和删除 @types/node,因为其他东西包括它,它只会一团糟。

最小示例(结构取自现有的 path.d.ts 文件):

declare module 'path' {
  namespace path {
    interface PlatformPath {
      normalize(p: absolutePath): absolutePath;
    }
  }
  const path: path.PlatformPath;
  export = path;
}

declare module 'node:path' {
  import path = require('path');
  export = path;
}

1 个答案:

答案 0 :(得分:0)

回答自己以供参考:

我试图复制 @types/node 文件执行此操作的方式,但没有成功。只需导出一个界面即可:

declare module 'path' {
  export interface PlatformPath {
    normalize(p: absolutePath): absolutePath;
    normalize(p: string): relativePath;
  }
}