Given a simple interface:
interface Schedule {
interval: "month" | "day";
price: number;
}
I can construct a template-literal type (using ts4.4):
type PrefixedScheduleKey = `schedule__${keyof Schedule}`; // "schedule__interval" | "schedule__price"
Let's say there are a few of such interfaces and I'd like to merge (essentially flatten) their prefixed, template-literal keys to end up with:
interface Metadata {
foo: string;
bar: string;
// computed
schedule__interval: "month" | "day";
schedule__price: number;
...
}
How should I declare such interface keys?
interface Metadata {
[key: PrefixedScheduleKey]: any
}
// throws
// An index signature parameter type cannot be a literal type or generic type. Consider using a mapped object type instead.(1337)
And
interface Metadata {
[key in `schedule__${keyof Schedule}`]: any
}
// leads to
// A computed property name must be of type 'string', 'number', 'symbol', or 'any'.(2464)
// Member '[key in `schedule__${keyof' implicitly has an 'any' type.(7008)
Here's the playground.
BTW, it was suggested on github to use type instead of interface but that won't work because my Metadata interface already extends another interface but I tried to keep the example simple.