I've put together this sandbox of code
In my project, there are 2 functions named slice
in separate classes. They both contain a lengthy and repetitive set of overrides:
export class Atom<S> {
constructor(private initial: S) {
}
// Several more slice overrides go here...
If I have to modify these overrides, it will require changing the code in 2 different places.
Is there a way to reuse a single function across two different areas?
I attempted to create a generic slice
function
export function slice<A, S, Key extends keyof A>(atom: A, key: Key): Slice<A[Key], S>;
export function slice<A, S, Key1 extends keyof A, Key2 extends keyof A[Key1]>(atom: A, key1: Key1, key2: Key2): Slice<A[Key1][Key2], S>;
// More slice function overrides...
And then calling it like this:
slice<T>(...keys: Key[]): Slice<T, S> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return slice(this, keys as any);
}
However, the typing information is lost without the overloads specified on each function.