We can use the "declare module" to extend the type of module.
eg:
// 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
But this is an extension of functionality.
Now I want to extend fetch itself.
I want to have this effect:
// src/index.ts
import fetch from 'node-fetch';
fetch.foo();
The declaration file for node-fetch itself looks like this:
// 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;
How can I extend the type of fetch?
I tried it. It doesn't work:
// 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
}
How would you like to solve it, please.
Thanks!
I believe what you need to do is use the interface keyword as shown here: https://stackoverflow.com/a/56861261/106625 –