I am attempting to modify the alpha
method in the context of the Cat
class, and have the beta
method reflect those modifications.
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 })
The error lies in the bar: 3
parameter
The argument '{ foo: string; bar: number; }' is not compatible with the parameter '{ foo: string; }'. Object literal can only define known properties, and 'bar' is not present in type '{ foo: string; }'.(2345)
Is there a way to update the beta
method without duplicating it? Maybe using a decorator?
One potential solution could be:
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 })
Another idea:
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)
}