I created a FileHandler class with methods to read a file or many files like this.
export class FileHandler {
private static async readFileFromFileQuery (fq: FileQuery): Promise<File> {
const { path, encoding, flag } = FileQueryHandler.make(fq);
const content = await promisify(fs.readFile)(path, { encoding, flag })
return { path, encoding, flag, content };
}
static async readFile (a: Path | FileQuery, b?: Omit<FileQuery, 'path'>): Promise<File> {
if (typeof a === 'string') a = FileQueryHandler.getFromPath(a, b);
return FileHandler.readFileFromFileQuery(a);
}
static async readFiles (a: (Path | FileQuery)[] | Directory, b?: Omit<FileQuery, 'path'>): Promise<File[]> {
if (a instanceof Array) return Promise.all(a.map(p => this.readFile(p, b)));
return FileHandler.readFiles(PathHandler.getFromDirectory(a), b);
}
static async readFilesFromDirectory(a: Path | FileQuery, b?: Omit<FileQuery, 'path'>): Promise<File[]> {
const ps = await DirectoryHandler.readDirectory(a);
if (typeof a === 'string') return await FileHandler.readFiles(ps, b);
return await FileHandler.readFiles(ps, a);
}
}
This is a class with static methods because I don't need them to be public.
What I would like to do now is expand upon this class.
I would like to wrap the FileHandler.readFile in a try catch and possibly return null, something like this, where the method is named readFile but the return value is different from the original.
export class FileOrNullHandler {
async readFile (a: Path | FileQuery, b?: Omit<FileQuery, 'path'>): Promise<File | null> {
return orNull(() => FileHandler.readFile(a, b));
}
}
I would also like to get all the other methods for free because what I see is all the original FileHandler are based on FileHandler.readFile.
I've tried some ways of converting both to not use static methods, and I've also experimented with return types, but nothing is jumping out at me as the simple way to do this.
What I need is the following:
- Have parity between methods within both classes
- Have all the return values represent the class functionality
I am looking for the best way to do this.
Ideally, the first thing that comes to mind would be I would be able to use
staticmethods that callthisReturnType<this.method>
But both of those things do not exist.
What I want is the following result:
FileHandler.readFilethat returnsFile | nullFileHandler.readFilesthat returnsFile[]FileHandler.readFilesFromDirectorythat returnsFile[]FileOrNullHandler.readFilethat returns(File | null)FileOrNullHandler.readFilesthat returns(File | null)[]FileOrNullHandler.readFilesFromDirectorythat returns(File | null)[]
classat all. Classes are meant to create instances, not to be used as a collection of static "methods". You are looking for TypeScript namespaces or JavaScript object literals.thisto refer to the methods looks good, you could then extend yourFileHandlerobject, using either inheritance withObject.createor a copy withObject.assign, and then overwrite thereadFilefunction. I have no idea how to type this properly in TypeScript though, so I won't post an answer.