I have the following function:
change(key: keyof McuParams, dispatch: Function) {
/** Body removed for brevity */
}
When I call the function...
this.change(VARIABLE_FROM_MY_API, this.props.dispatch)
... I (understandably) get the following error:
Argument of type 'string' is not assignable to parameter of type '"foo" | "bar"'
This makes sense since there is no way for the compiler to know what my API is sending at compile time. However, user defined type guards can sometimes be used to infer type information at runtime and pass that information to the compiler via conditionals.
Is it possible to write a user defined type guard for a keyof
string type such as keyOf foo
when foo
is defined ONLY as a type (and not in an array)? If so, how?