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

前端 未结 1 1901
情话喂你
情话喂你 2021-01-12 23:58

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

1条回答
  •  囚心锁ツ
    2021-01-13 00:20

    You can constrain the type to a specific enum.

    function foo(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.

    0 讨论(0)
提交回复
热议问题