简体   繁体   中英

How to constrain the type of generic parameter to a Typescript function to be an enum

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM