Can a class or object be created with type constraints between methods?
abstract class Example<T>{
abstract methodOne(): T
abstract methodTwo (arg: T):any
}
I am looking to ensure that the argument of methodTwo is the same type as the return type of methodOne - any differences should result in a type error. The specific type itself is not the main focus.
While I was able to achieve this using a generic class, I found it cumbersome to have to specify the type in advance, which feels unnecessary to me.
- Is there a way to accomplish this without needing to define the type beforehand, or without relying on a generic class?
- As an additional question - is it feasible to reference another method's class or return type? For example, something like
ormethodTwo(arg: ReturnType<this.methodOne>):any
?methodTwo(arg: typeof this.methodOne):any