-
-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Export Volume as a class instead of const #585
Conversation
I'm facing the same issue. Can this PR be merged? |
This PR makes sense to me. The use of |
I agree. As of now, we have to either use: This would be a small improvement that respects best-practices and reduces confusion. |
Closing due to age |
@G-Rath but the issue still exists. We can't use |
@ericmorand feel free to open a new issue with an updated reproduction - I've not saying there isn't a problem, but this PR was a couple of years old and frankly I'm pretty sure this could be a breaking change? Either way, I'm happy to discuss if someone opens a fresh issue with a modern reproduction (and ideally a TS playground link) |
@G-Rath, thanks for the answer. Not really a reproduction, but here is what we have to write to declare a method parameter as a import {Volume} from "memfs";
type CreateCompiler = (options: ts.CompilerOptions, volume?: InstanceType<typeof Volume>) => Compiler; By changing the declaration of import {Volume} from "memfs";
type CreateCompiler = (options: ts.CompilerOptions, volume?: Volume) => Compiler; For information, here is the modified declaration file that I used to build the code just above: import Stats from './Stats';
import Dirent from './Dirent';
import { Volume, StatWatcher, FSWatcher, IWriteStream, DirectoryJSON, NestedDirectoryJSON } from './volume';
import { constants } from './constants';
import type { FsPromisesApi } from './node/types';
import type * as misc from './node/types/misc';
export { Volume, DirectoryJSON, NestedDirectoryJSON };
export declare const vol: Volume;
export interface IFs extends Volume {
constants: typeof constants;
Stats: new (...args: any[]) => Stats;
Dirent: new (...args: any[]) => Dirent;
StatWatcher: new () => StatWatcher;
FSWatcher: new () => FSWatcher;
ReadStream: new (...args: any[]) => misc.IReadStream;
WriteStream: new (...args: any[]) => IWriteStream;
promises: FsPromisesApi;
_toUnixTimestamp: any;
}
export declare function createFsFromVolume(vol: Volume): IFs;
export declare const fs: IFs;
/**
* Creates a new file system instance.
*
* @param json File system structure expressed as a JSON object.
* Use `null` for empty directories and empty string for empty files.
* @param cwd Current working directory. The JSON structure will be created
* relative to this path.
* @returns A `memfs` file system instance, which is a drop-in replacement for
* the `fs` module.
*/
export declare const memfs: (json?: NestedDirectoryJSON, cwd?: string) => {
fs: IFs;
vol: Volume;
};
export type IFsWithVolume = IFs & {
__vol: Volume;
}; As you can see, all I did was remove the |
Also just fair warning, one of my first questions will be "what's the problem with just doing |
@G-Rath , actually, The reason is that The main issue is that import type {Volume} from "memfs"; // this is the type
import {Volume} from "memfs"; // this is the class _and_ the type I personally always declare interfaces independently of the type of factory that I expose (a class or a classic functional factory). This definitely solves the issue upstream. But exposing a class also works thanks to TypeScript (deliberate) merge between class and type: https://www.typescriptlang.org/docs/handbook/declaration-merging.html#basic-concepts |
I ran into a TS error with the following code:
Using the suggested
typeof
doesn't help either:The strange thing is that
Volume
is already exported astypeof Volume
. However, it seems incorrect because in TSas suggested in the Handbook. Indeed, if I import the class directly, the error goes away:
Any thoughts? Thanks.