In my code, there is a TypeScript enum defined like this:
enum UIConfigurationType {
DisplayTableFields = "display-table-field",
FormFields = "form-field",
MainAttributes = "main-attribute",
SearchAttributes = "search-attribute",
}
Along with a conditional type that looks like this:
type UIConfigurationEntitySuffix<T extends UIConfigurationType> =
T extends UIConfigurationType.DisplayTableFields
? "display-table-fields"
: T extends UIConfigurationType.FormFields
? "form-fields"
: T extends UIConfigurationType.MainAttributes
? "main-attributes"
: T extends UIConfigurationType.SearchAttributes
? "search-attributes"
: never;
This conditional type should statically return corresponding literal types based on the input. For example, it correctly returns "search-attributes"
for
UIConfigurationType.SearchAttributes
.
Furthermore, I have a function in place that dynamically maps these values as per the conditional type rules:
function getUIConfigurationEntitySuffix<T extends UIConfigurationType>(
uiConfigurationType: T
): UIConfigurationEntitySuffix<T> {
switch (uiConfigurationType) {
case UIConfigurationType.DisplayTableFields:
return "display-table-fields";
case UIConfigurationType.FormFields:
return "form-fields";
case UIConfigurationType.MainAttributes:
return "main-attributes";
case UIConfigurationType.SearchAttributes:
return "search-attributes";
default:
throw new Error(`Unknown uiConfigurationType: ${uiConfigurationType}`);
}
}
However, TypeScript throws error messages for each case in this function:
View TypeScript errors on getUIConfigurationEntitySuffix
function
Can anyone spot what I might be missing here? Your help is much appreciated!