Hi,
We're building out a function to parse a string into an object and ideally use generics to define what is being returned.
Rather than trying to give loads of context etc, hopefully I can more easily explain what we're trying to achieve with an example:
type Obj = Obj1 | Obj2 | Obj3;
type Obj1 = {
readonly version: 1;
// custom attributes for Obj1
}
type Obj2 = {
readonly version: 2;
// custom attributes for Obj2
}
type Obj3 = {
readonly version: 3;
// custom attributes for Obj3
}
// possible example 1
function parse1<TObj extends Obj>(body: string): TObj {
// how can I access/reference the TObj version as it dictates how I parse the string?
}
// possible example 2
function parse2<TObj extends Obj, TVersion = TObj["version"]>(body: string): TObj {
// how can I access/reference TVersion as it dictates how I parse the string?
}
If anymore information is needed, I'm happy to give it.
Thanks,
Gary