I am trying to make changes to the alpha method within Cat and have beta reflect those changes.
const wrapperFn = <T extends (...args: any) => any> (a: T) => {
return (...args: Parameters<T>) => {
return ''
}
}
class FourLegged {
alpha = ({ foo }: { foo: string }) => foo
beta = wrapperFn(this.alpha)
}
class Cat extends FourLegged {
alpha = ({ foo, bar }: { foo: string, bar?: number}) => (foo + bar)
}
const c = new Cat()
c.beta({ foo: 'foo', bar: 3 })
bar: 3 has an error
Argument of type '{ foo: string; bar: number; }' is not assignable to parameter of type '{ foo: string; }'. Object literal may only specify known properties, and 'bar' does not exist in type '{ foo: string; }'.(2345)
Is there some way to update this beta without copying it over? Perhaps a decorator?
Here's one possible idea:
const wrapperFn = <T extends (...args: any) => any> (a: T) => {
return (...args: Parameters<T>) => {
return ''
}
}
const example = (value: typeof replacements) => class FourLegged {
replace = value()
alpha = ({ foo }: { foo: string }) => foo
beta = wrapperFn(this.replace.alpha || this.alpha)
}
const replacements = () => {
const alpha = ({ foo, bar }: { foo: string, bar?: number }) => (foo + bar)
return { alpha }
// return { alpha: null }
}
class Cat extends example(replacements) { }
const c = new Cat()
c.beta({ foo: 'foo', bar: 1 })
Idea 2:
abstract class FourLegged <replaceAlpha extends (...args: any) => any> {
replaceAlpha: replaceAlpha | null = null
alpha = ({ foo }: { foo: string }) => foo
beta = wrapperFn(this.replaceAlpha || this.alpha)
}
class Cat extends FourLegged<Cat['replaceAlpha']> {
replaceAlpha = ({ foo, bar }: { foo: string, bar?: number }) => (foo + bar)
}