Typescript - 无法扩充模块

Fen*_*1kz 5 twitter node.js typescript

我正在使用 typescript v3.6.4 和 twitter API ( twit )开发一个个人项目。

我还@types/twithttps://www.npmjs.com/package/@types/twit安装

我想向“列表/成员”端点发出请求。

我的代码是:

import Twit from "twit";

const client = new Twit(...);

client.get('lists/members', {list_id: '123123'})
Run Code Online (Sandbox Code Playgroud)

但是,打字稿给了我一个错误:

src/data/TwitterProvider.ts:16:34 - error TS2769: No overload matches this call.
  Overload 1 of 3, '(path: string, callback: Callback): void', gave the following error.
    Argument of type '{ list_id: string; }' is not assignable to parameter of type 'Callback'.
      Object literal may only specify known properties, and 'list_id' does not exist in type 'Callback'.
  Overload 2 of 3, '(path: string, params?: Params): Promise<PromiseResponse>', gave the following error.
    Argument of type '{ list_id: string; }' is not assignable to parameter of type 'Params'.
      Object literal may only specify known properties, and 'list_id' does not exist in type 'Params'.

     client.get('lists/members', {list_id: 'test'})
Run Code Online (Sandbox Code Playgroud)

这是有道理的,因为https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/twit/index.d.ts文件中没有list_id属性

我做了一些研究并创建了./src/@types/twit.d.ts

import "twit";
declare module 'twit' {
  namespace Twit {
    interface Params {
      list_id?: string;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但是我仍然遇到同样的错误。

我的 tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "dist",
    "rootDir": "src",
    "sourceMap": true
  },
  "typeRoots": [
    "src/@types",
    "node_modules/@types"
  ],
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}
Run Code Online (Sandbox Code Playgroud)

我正在运行代码ts-node src/index.ts

for*_*d04 10

您的模块增强方法通常适用于“标准”npm 包声明。不幸的是,如果twit模块增强是不可能的(或者我不知道正确的方法)。

Twitexport = Twit 通过语法导出为 CommonJS 模块。

默认导出旨在替代此行为;然而,两者是不相容的。TypeScript 支持 export = 来对传统 CommonJS 和 AMD 工作流程进行建模。

TypeScript 显然只允许 ES 模块的模块扩充(参见下面的示例),上面的语法显式创建了 Node CommonJS 默认导出。该 Node 模块系统实现在某些方面偏离了原始 CommonJS 标准,例如用于 Babel 和 TypeScript 编译器输出的标准。

例如,Node 实现允许通过 导出单个默认对象,而 CommonJS 规范只允许向对象modules.exports = ...添加属性和方法,例如(有关 ES 和 CommonJS 模块导入转换的更多信息,请参见此处此处exportsexport.foo = ...

tl;dr:我测试了 Node CommonJS 导出的模块增强(此处忽略模块内的名称空间,因为它是反模式)。

lib.d.ts:

declare module "twit3" {
  class Twit3 { bar(): string; }
  export = Twit3;
}
Run Code Online (Sandbox Code Playgroud)

索引.ts:

import Twit3 from "twit3";

// Error: Cannot augment module 'twit3' because it resolves to a non-module entity.
declare module "twit3" {
  class Twit3 { baz(): string; }
}
Run Code Online (Sandbox Code Playgroud)

代码沙箱

...没有成功。export =用命名导出替换语法使示例可以编译(默认导出通常无法增强)。

怎么解决呢?

twit如果确实缺少选项,请为其创建一个票证/PR Params。同时,像这样的解决方法可以保留附加属性的强类型,同时仍然list_id向运行时添加选项:

const yourOtherParams: Params = {/* insert here */}
client.get("lists/members", { ...yourOtherParams , ...{ list_id: "123123" } });

// or cast directly
client.get("lists/members", { list_id: "123123" } as Params);
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!