In my scenario, I am dealing with two enums and a key-value store. The first level of the store is identified by Enum1
, while the second level is identified by Enum2
as illustrated below.
enum X { x, y }
enum Y { z, w }
const DATA: { [key in X]?: { [key in Y]?: string } = {
[X.x]: {
[Y.z]: "test"
}
}
I am aiming to define a method type signature that can verify whether a specific combination of enums is valid.
I believe it should resemble something like this:
const validateCombo = <E1 extends X, E2 extends Y>(
arg1: E1,
arg2: E2,
value: (typeof DATA)[E1][E2] // This is where I run into issues
// Error message: Type 'Y' cannot be used to index type <big object type redacted>
)
The above example has been simplified for explanation purposes. Even though the provided use case may seem redundant or unrealistic, the essence lies in clarifying the core logic.
Hence, considering the given DATA
variable, here are the expected outcomes:
validateCombo(X.x, Y.z, "test") // no error, since it exists in data
validateCombo(X.x, Y.w, "test") // error, combination does not exist in data
validateCombo(X.x, Y.z, "result") // error, value does not exist in data