Keyof nested child objects

后端 未结 2 526
你的背包
你的背包 2020-12-03 17:52

I have a recursively typed object that I want to get the keys of and any child keys of a certain type.

For instance. Below I want to get a union type of:

<         


        
2条回答
  •  庸人自扰
    2020-12-03 18:08

    Here's an infinitely recursive solution:

    type Paths = T extends RouteList
      ? keyof T | { [K in keyof T]: Paths }[keyof T]
      : never
    
    type ListPaths = Paths // -> "/parent" | "/another" | "/child"
    

    Tested on Typescript v3.5.1. Also you need to remove the type annotation from the list variable as advised by @jcalz.

提交回复
热议问题