I am trying to make an enrich-object function, but I can't figure out how to change the type of the values inside a Type.
So given the following types
{ a: string, c: number }
{ a: boolean, b: Cow }
I want the returned type to respectively be
{ a: Container<string>, c: Container<number> }
{ a: Container<boolean>, b: Container<Cow> }
The closest I've gotten is using the Record util
enrich<T>(value: T) {
// create mapped object by iterating over object keys
return container as Record<T, Container<any>>
}
But that changes all of the objects value types to Container<any>
, erasing part of the context from the input.
You can use mapped types for this:
type Enrich<T> = {[key in keyof T]: Container<T[key]>};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With