Is it possible to achieve something similar in TypeScript?
type Something = {...}
interface A extends Something {...}
interface B extends Something {...}
interface MyInterface<T extends Something> {
method(): T
anotherMethod(): number | number[]
}
The return type of anotherMethod()
is dependent on the generic, so:
- If T is type A, then
anotherMethod() => number
- If T is type B, then
anotherMethod() => number[]
For example:
const myObjA: MyInterface<A> = {}
myObjA.anotherMethod() // ==> returns a number
const myObjB: MyInterface<B> = {}
myObjB.anotherMethod() // ==> returns an array
Does this question make sense?
Thank you, Fran