Is there a method to utilize multiple generic parameters as object keys in TypeScript?
I came across this answer which works well when there is only one parameter, but encounters issues with more than one. The error message "A mapped type may not declare properties or methods" pops up if attempting to declare more than one [key in T]
.
Here's an example of a class I have:
export class DynamicForm<
K0 extends string, V0,
K1 extends string, V1,
...
> {
public get value(): {
[key in K0]: V0;
[key in K1]: V1; // ERROR: A mapped type may not declare properties or methods. ts(7061)
...
} {
// returning value...
}
public constructor(
input0?: InputBase<K0, V0>,
input1?: InputBase<K1, V1>,
...
) {
// doing stuff...
}
}
In essence, I aim to be able to retrieve a typed value based on the generic types passed into the constructor.
I could potentially use a [key in ClassWithAllKeys]
, but that might disconnect K0 <=> V0
, K1 <=> V1
, etc.