I'm trying to right a type safe config builder for partial types. It'll be handy for setting and overriding default params for testing.
I have this so far:
const getPartial = <T>(defaultParameters: Partial<T>) =>
(overrideParameters: Partial<T>): Partial<T> => ({
...defaultParameters,
...overrideParameters
});
Which works fine for simple types such as:
interface IMessage {
id: number
message: string
}
const getMessage = getPartial<IMessage>({
id: 1,
message: 'Hello'
});
console.log(getMessage({
message: 'Hi'
}));
But if I have a child type with another generic, or multiple generics, I'm not sure the best pass that down the chain.
interface IWithGeneric<T extends {}> {
id: number
data: Partial<T> // Need to set as Partial to avoid "Type '{}' is not assignable to type 'T'. 'T' could be instantiated with an arbitrary type which could be unrelated to '{}'." error
}
// Need to curry the function again to have something to hook the generics on.
const getWithGeneric = <T>() => getPartial<IWithGeneric<T>>({
id: 1,
data: {},
});
console.log(getWithGeneric<{ message: string }>()({
id: 1,
data: {
message: 'hello',
}
}));
Is there a way I can pass through multiple variable generic types without currying the function and/or without needing a Partial?
Would really appreciate it if someone can point me in the right direction.