To accomplish this task, you can create a "generator" generic type that will provide the desired result.
type First = {
attribute1: string;
attribute2: string;
};
type Second = {
property1: string;
property2: string;
}
type MergeKeys<FK, SK> =
FK extends string ?
SK extends string ?
`${FK}${Capitalize<SK>}`
: never
: never;
type CombineObjects<F,S> = {
[key in MergeKeys<keyof F,keyof S>]: string;
};
type Result = CombineObjects<First, Second>;
The MergeKeys
type simply combines the provided key values.
The CombineObjects
type is a straightforward generic type that receives two parameters and produces a type where the keys are merged using keyof
on both input parameters.
It would be advisable to enhance these types further to ensure only objects can be passed as arguments and to achieve complete type coverage :)
For a functional example, please refer to the code snippet in the Typescript Playground