TypeScript: how to extract the generic parameter from a type?

后端 未结 3 1986
Happy的楠姐
Happy的楠姐 2020-12-01 20:56

Say I have a type like React.ComponentClass and I want to reference the Props part but it is unnamed in my current context.

To

3条回答
  •  醉梦人生
    2020-12-01 21:36

    You can use infer:

    type TypeWithGeneric = T[]
    type extractGeneric = Type extends TypeWithGeneric ? X : never
    
    type extracted = extractGeneric>
    // extracted === number
    

    Playground

提交回复
热议问题