My global type definition consists of an object with keys (referred to as a prop
) that must be passed by the user as a parameter to myFunction()
, and values that are used solely for type checking within my code.
These values can fall into only 3 "possible types" (contained in Possible
). Each possible type has its own distinct definition.
I want myFunction()
to only accept props
whose value's test
property is equal to "possible1"
.
The generic type OnlyPossible1Props<T>
demonstrates how I envisioned implementing this, but unfortunately it does not function as intended.
Initially, I encounter an error:
Type 'prop' cannot be used to index type 'T'. (2536)
In addition, instead of returning "abc" | "def"
as expected in myFunction()
, it returns string | number
.
The closest workaround I've managed so far is the section of code that I have commented out. However, along the way, it loses key information because it isn't stored anywhere.