I have a function that takes an object as a model and an object that represents a mapping. It returns an object with two properties: one representing the base model and one that represents the model with every property wrapped in another type. The mapping determines the keys of the returned object, and the mapping will always be
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
}
}
so calling this function like so:
fooBar({ message: 'hello' }, { foo: 'thing', bar: 'thing$' })
should return a type something like this:
type MyType = {
thing: { message: string },
thing$: WrappedType<{ message: string }>
}
But I cannot figure out how to substitute property names from an object and still be type checked correctly.
I followed along with this question but then the type of the returned properties were { message: string } | WrappedType<{ message: string }>
. Is there a way to pull this off without the result being a union type?