I have two interfaces as follows:
interface Foo {
foo: string;
}
interface Bar {
prop1: string;
prop2: string;
}
My goal is to create a type that combines these two interface keys with underline between them, something like this:
type MergeKeys<A,B> = {
[P in keyof A + '_' + P2 in keyof B]: string;
};
type Result = MergeKeys<Foo,Bar>;
So that the result would be:
interface Result {
foo_prop1: string;
foo_prop2: string;
}
Is this even possible?
Here's one way to do it:
The main idea is to use template literal types to perform the desired string concatenation.
The only wrinkle is that the compiler will reject the simpler
`${keyof A}_${keyof B}`, because TypeScript supportssymbol-valued keys which cannot be serialized to strings via template literals. In order to convince the compiler to serializekeyof A, we have to get rid of the possibility that anysymbol-valued keys will be in there. That's where theExclude<T, U>utility type comes in. The typeExclude<keyof A, symbol>will return the union of all the keys ofAwhich are notsymbols (so you'll get anystringornumberliteral types but nosymbols).Okay, let's test it:
Looks good!
Playground link to code