I though this was very straightforward bu apparently I was wrong. How can I create a simple conditional type based on an enum key? like this:
export enum inputTypes {
text = 'text',
email = 'email',
password = 'password',
number = 'number',
date = 'date',
textarea = 'textarea',
dropdown = 'dropdown',
checkbox = 'checkbox',
}
export interface IInput {
id: string,
type: inputTypes,
name: string,
}
And the conditional type based on the inputType:
export type onInputChange =
IInput['type'] extends inputTypes.dropdown
? DropDownChangeEventHandler
: FormChangeEventHandler;
I've tried some variations with keyof typeof keyof typeof And could not reach what I am trying.
The end result that I need is that if the input type is dropdown I'd like to have a different change event. So my type onInputChange would be equal to: DropDownChangeEventHandler | FormChangeEventHandler, and later in the code, do a type guard and get the proper type for the dropdown change event.
onInputChangetype at runtime, which is not possible with TypeScript.DropDownChangeEventHandler | FormChangeEventHandlerIInput['type'] extends inputTypes.dropdownis a compile-time check that will only run once.