如何获取数组项的类型? [英] How to get type of array items?

查看:27
本文介绍了如何获取数组项的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个类型 type foo = Array<{ name: string;测试:数量;}>,是否可以获取数组中值的类型,在本例中为接口.我知道有 keyof 可以获取密钥,值是否有类似的东西?

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 interface. I know there is keyof to get the keys, is there something similar for values?

推荐答案

如果您正在寻找如何提取 { name: string;测试:数量;} 类型,您可以简单地为索引项"创建别名:

If you're looking to how to extract { name: string; test: number; } type, you can simply create alias to "item at index":

type Foo = Array<{ name: string; test: number; }>;
type FooItem = Foo[0];

type FooItem = Foo[number];

这篇关于如何获取数组项的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆