我们可以使用 "声明模块" 来扩展模块的类型。
例如:
// typings/node-fetch.d.ts
import 'node-fetch';
declare module 'node-fetch' {
function kk(): void;
}
// src/index.ts
import fetch, { kk } from 'node-fetch';
kk(); // ok
但这是功能的扩展。
现在我想扩展 fetch 本身。
我想有这样的效果:
// src/index.ts
import fetch from 'node-fetch';
fetch.foo();
节点获取本身的声明文件如下所示:
// node_modules/@types/node-fetch/index.d.ts
declare function fetch(
url: RequestInfo,
init?: RequestInit
): Promise<Response>;
declare namespace fetch {
function isRedirect(code: number): boolean;
}
export default fetch;
如何扩展抓取类型?
我试过了。它不起作用:
// typings/node-fetch.d.ts
import fetch from 'node-fetch';
declare module 'node-fetch' {
namespace fetch {
function foo(): void;
}
export default fetch; // Error, Exports and export assignments are not permitted in module augmentations
}
请问您想如何解决它。
谢谢!
我相信你需要做的是使用界面关键字,如下所示:https://stackoverflow.com/a/56861261/106625 –