I have a scenario where I want to have a function that can accept any number of args of an generic object.
I want the result to return a tuple where each of the generic params of this object is the tuple position type.
Example
type Wrap<T> = {
obj: T;
}
function UnWrap<T extends Array<Wrap<One|Two>>>(...args:T){ //return type?
return args.map(i => i.obj);
}
type One = {
foo: string;
}
type Two = {
bar: string;
}
let one: Wrap<One> = {obj: {foo: 'abc'}}
let two: Wrap<Two> ={obj: {bar: 'abc'}}
// res type should be [One, Two, Two, One]
let res = UnWrap(one, two, two, one)
I can get the type to work if I just return the exact type passed in:
function ReturnSelf<T extends Array<Wrap<One|Two>>>(...args:T): typeof args{
return args;
}
But I'm not sure how to index the ['obj']
type. I think maybe mapped types can be used to do this, but I can't quite figure it out.