How to get type of array items?

后端 未结 5 1048
有刺的猬
有刺的猬 2020-11-29 10:03

If I have a type type foo = Array<{ name: string; test: number; }>, would it be possible to get the type of the values within the array, in this case, the

5条回答
  •  难免孤独
    2020-11-29 10:36

    Despite Aleksey answer, it might be useful to know that if the instance of that generic type exposes at least one member of the type you want to extract, you could use typeof to query the type of that member.

    For a generic Array the type can be queried from any array item:

    Note that line 27 only exists at design time so that will not generate any errors even if arr is empty or undefined at runtime.

提交回复
热议问题