Let's say I have a class with a bunch of methods with the same signature.
class Foo {
method1(foo: string): number { return 42; }
method2(foo: string): number { return 99; }
}
I can define an interface for the method signature:
interface Method { (foo: string) => number; }
I would like to avoid repeating the signature over and over again for each method. I know that if I am assigning a variable I can say
const fn: Method = foo => 99;
But how can I do this when defining a method? I would like to get the equivalent of
class Foo {
method1: Method(foo) { return 42; }
method2: Method(foo) { return 99; }
}
but that obviously doesn't work.
But how can I do this when defining a method
Just like you did with a variable
class Foo {
method1: Method = (foo) => { return 42; }
method2: Method = (foo) => { return 99; }
}
NOTE: this does make it a member (instead of the method) but the performance implications are insignificant.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With