My types implementation is structured as follows:
type GenericType<T, K extends keyof T = keyof T> = {
name: K;
params: T[K]
}
type Params = {
a: 1;
b: 2;
}
const test: GenericType<Params> = {
name: "a",
params: 2
}
When I create an object like test
with the property name: "a"
, I would like the type of params
to be automatically inferred so that params
must be 1
. Currently, params
has type 1 | 2
, which corresponds to Params[keyof Params]
. However, I believe it should be feasible to restrict the type of params
to just 1
based on the value of name
, without explicitly specifying the second generic type such as
const test: GenericType<Params, "a">
. Essentially, my desired structure is:
type GenericType <T> = {
name: keyof T;
params: T[value of name]
}
Do you think achieving this behavior is possible in TypeScript?