Assuming that the value of a member (single) is statically known (i.e. a literal boolean in this case), is it possible to pick the return type of another member method based on that?
interface I {
single: boolean;
// Want the return type to be 'number'
// if single === true and 'Set<number>' otherwise.
returnSingle: () => number | Set<number>
}
Edit: would this be correct?
type SingleOrSet<C extends boolean> = C extends true ? number : Set<number>;
interface I<T extends boolean> {
single: T;
// Want the return type to be 'number'
// if single === true and 'Set<number>' otherwise.
returnSingle: () => SingleOrSet<T>
}
const p: I<true> = {single: true, returnSingle: () => {return 1}};
Is there a way for T to be inferred in usage so it doesn't have to be passed as a type argument to I?