如何使用打字稿支持同一模块的多个版本?

And*_*091 5 javascript types node.js typescript discord.js

我正在制作一个打字稿 npm 包。
它使用discord.js,但 discord.js有两个主要版本:
*
discord.js 11.5 * discord.js v12.0.0-dev

我希望我的模块支持这两个版本。我的意思是,用户可以安装他们想要的 discord.js 版本,并且包将使用好的代码。

对于另一个项目,在 javascript (没有打字稿)中,我使用这个:

const { version } = require("discord.js");
if(version === "12.0.0-dev"){
    // code for v12
} else {
    // code for v11
}
Run Code Online (Sandbox Code Playgroud)

它完美无缺。但是,对于打字稿,由于打字,它更加复杂。事实上,discord.js 类型在 v11 和 v12 中是不一样的,所以我不能用两个版本编译代码:

const { Guild, version } = require("discord.js");
if(version === "12.0.0-dev"){
    Guild.iconURL(); // for v12
} else {
    Guild.iconURL; // for v11
}
Run Code Online (Sandbox Code Playgroud)

如果安装了V12,它会抛出iconURL() doesn't exist on Guild,如果它是V11, iconURL doesn't exist on Guild
有没有办法在同一个包中支持两个版本,而无需为每个版本创建一个分支?

che*_*som 2

虽然并不理想,但您可以手动下载 v11 和 v12 的类型并将模块名称更改为如下所示:

// discord.js@11.d.ts
declare module 'discord.js@11' {
  ...
}

// discord.js@12.d.ts
declare module 'discord.js@12' {
  ...
}
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样导入类型:

import { Guild as Guild12, version } from "discord.js";
import { Guild as Guild11 } from "discord.js@11";

declare const guild: Guild11 | Guild12 // obviously wouldn't be declared like this in your actual code

// At the time of writing v12.0.2 has been released
if (version === "12.0.2") {
    (guild as Guild12).iconURL(); // for v12
} else {
    (guild as Guild11).iconURL; // for v11
}
Run Code Online (Sandbox Code Playgroud)

您还可以定义类型保护:

function isV12(guild: Guild11 | Guild12): guild is Guild12 {
  return version === "12.0.2";
}

if (isV12(guild)) {
  guild.iconURL();
} else {
  guild.iconURL();
}
Run Code Online (Sandbox Code Playgroud)

我尝试像这样使用别名discord@11 和discord@12 package.json

// discord.js@11.d.ts
declare module 'discord.js@11' {
  ...
}

// discord.js@12.d.ts
declare module 'discord.js@12' {
  ...
}
Run Code Online (Sandbox Code Playgroud)

但我得到了这个错误:

import { Guild as Guild12, version } from "discord.js12";
//                                        ~~~~~~~~~~~~~~
// with pnpm:
// File '/path/to/project/node_modules/.pnpm/registry.npmjs.org/discord.js/12.0.2/node_modules/discord.js/typings/index.d.ts' is not a module. (ts2306)
// with npm:
// File '/path/to/project/node_modules/discord.js12/typings/index.d.ts' is not a module. (ts2306)
Run Code Online (Sandbox Code Playgroud)