Is it possible to constrain the type of an input parameter to a generic Typescript function to be a Typescript enum?
In other words, I want to enforce that T in the function below is a non-constant Typescript enum
Foo<T>() { /* */ }
You can constrain the type to a specific enum.
function foo<T extends MyEnum>(param: T) {
}
However, you cannot constrain it to be any enum (see specs).
In addition to generic interfaces, we can also create generic classes. Note that it is not possible to create generic enums and namespaces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With