I am working on a function that accepts two objects as parameters: one representing a model and the other representing a mapping. The function then returns an object with two properties: one showing the base model and the other displaying the model with each property wrapped in another type. The key-value pairs of the returned object are determined by the mapping provided, which will always follow this structure:
type Mapping<Model> = {
foo: string,
bar: string
}
function fooBar<M extends object, B extends Mapping>(model: M, mapping: B) {
return {
[mapping.foo]: model,
[mapping.bar]: recursiveWrap(model) //this function would go through the model and wrap each property
}
}
For example, calling this function like so:
fooBar({ message: 'hello' }, { foo: 'thing', bar: 'thing$' })
should result in a type similar to this:
type MyType = {
thing: { message: string },
thing$: WrappedType<{ message: string }>
}
However, I am struggling to substitute property names from an object while maintaining proper type checking.
I tried following this question, but it led to the returned properties having a union type of
{ message: string } | WrappedType<{ message: string }>
. Is there a way to achieve this without resulting in a union type?