When importing a CommonJS module in a Typescript source, an object containing exported features of the module is received.
In a specific use case, the declarations for NodeJS's fs
module declare the exports as (typescript-) module, not as type. An interface declaration is needed for that module in order to pass the module object without losing the type information or to extend the module.
The desired action is:
import * as fs from "fs";
doSomethingWithFs(fsParameter: InstanceType<fs>) {
...
}
However, this results in:
TS2709: Cannot use namespace 'fs' as a type.
Is there a way to obtain a type from a module declaration without manually refactoring the typings?
EDIT: @Skovy's solution works perfectly:
import * as fs from "fs";
export type IFS = typeof fs;
// IFS can now be used as if it were declared as interface:
export interface A extends IFS { ... }
Thanks!