You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a follow-up to the conversation that happened on #585.
To summarize: as of today, memfs exports the Volume class as a const value, instead of a class. This makes using the type less elegant and more complicated than needed:
In this example, I have to use the non-obvious InstanceType<typeof Volume> type to make sure that the compiler accepts a Volume instance as argument for the method. Using the more obvious Volume type is not accepted by the compiler because it is a value:
$ tsc src/lib/compiler.ts
src/lib/compiler.ts:60:14 - error TS2749: 'Volume' refers to a value, but is being used as a type here. Did you mean 'typeof Volume'?
60 volume?: Volume
~~~~~~
Note that my testing with the declaration file emitted by memfs did demonstrate that exporting Volume as a class doesn't create any breaking change:
InstanceType<typeof Volume> continues to work - which means that people that are already using this syntax would not be impacted
No memfs method used by my compiler showcased any issue
For reference, the modified declaration file that I've used to conduct my test is this one:
importStatsfrom'./Stats';importDirentfrom'./Dirent';import{Volume,StatWatcher,FSWatcher,IWriteStream,DirectoryJSON,NestedDirectoryJSON}from'./volume';import{constants}from'./constants';importtype{FsPromisesApi}from'./node/types';importtype*asmiscfrom'./node/types/misc';export{DirectoryJSON,NestedDirectoryJSON,Volume};exportdeclareconstvol: Volume;exportinterfaceIFsextendsVolume{constants: typeofconstants;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;}exportdeclarefunctioncreateFsFromVolume(vol: Volume): IFs;exportdeclareconstfs: 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. */exportdeclareconstmemfs: (json?: NestedDirectoryJSON,cwd?: string)=>{fs: IFs;vol: Volume;};exporttypeIFsWithVolume=IFs&{__vol: Volume;};
The only changes are the removal of the _Volume alias, the re-export of Volume and the refactoring due to the removal of _Volume. Basically, exactly what the mentionned PR does.
The text was updated successfully, but these errors were encountered:
This is a follow-up to the conversation that happened on #585.
To summarize: as of today,
memfs
exports theVolume
class as aconst
value, instead of a class. This makes using the type less elegant and more complicated than needed:In this example, I have to use the non-obvious
InstanceType<typeof Volume>
type to make sure that the compiler accepts aVolume
instance as argument for the method. Using the more obviousVolume
type is not accepted by the compiler because it is a value:If
Volume
was exported as a class, it would become possible to use it as a type as well as a value (the class itself), as per TypeScript declaration merging rule: https://www.typescriptlang.org/docs/handbook/declaration-merging.html#basic-conceptsNote that my testing with the declaration file emitted by
memfs
did demonstrate that exportingVolume
as a class doesn't create any breaking change:InstanceType<typeof Volume>
continues to work - which means that people that are already using this syntax would not be impactedFor reference, the modified declaration file that I've used to conduct my test is this one:
The only changes are the removal of the
_Volume
alias, the re-export ofVolume
and the refactoring due to the removal of_Volume
. Basically, exactly what the mentionned PR does.The text was updated successfully, but these errors were encountered: