假设我有一些数组类型T[],是否可以T在另一个别名/接口中提取类型?例如,我的(假的)理想代码如下:
// for illustration only...
type ArrayElement<T[]> = T;
// then, ArrayElement<string[]> === string
Run Code Online (Sandbox Code Playgroud)
如果不是,是否存在不允许此类运营商的一般类型理论原因?如果没有,我可能会建议添加它.
谢谢!
zil*_*nas 56
通过在括号中写入索引类型可以很容易地实现这一点:
type ArrayElementType = ArrayType[number];
Run Code Online (Sandbox Code Playgroud)
更新
正如下面的评论所建议的,创建通用数组元素类型:
type ArrayElementType<ArrayType extends Array> = ArrayType[number];
Run Code Online (Sandbox Code Playgroud)
PSnumber不是一个值而是一个类型。
jer*_*ico 45
另一种选择:
type ArrayElement<A> = A extends readonly (infer T)[] ? T : never
Run Code Online (Sandbox Code Playgroud)
Wil*_*den 22
您可以通过以下方式实现这一目标:
type ArrayElement<ArrayType extends readonly unknown[]> = ArrayType[number];
Run Code Online (Sandbox Code Playgroud)
虽然我亲自给它起了类似的东西ArrayType extends readonly unknown[],因为ArrayType读起来要比它清楚得多readonly unknown[].
art*_*tem 17
从2.1开始,typescript支持类型的[]运算符.官方名称是索引访问类型,也称为查找类型,它的工作方式如下:
type A = {a: string, b: number} [];
type AElement = A[0];
let e: AElement = {x: 0}; //error TS2322: Type '{ x: number; }' is not
//assignable to type '{ a: string; b: number; }'
Run Code Online (Sandbox Code Playgroud)
小智 6
另外一个选择:
type Flatten<Type> = Type extends Array<infer Item> ? Item : Type;
Run Code Online (Sandbox Code Playgroud)
您可以在以下位置找到更多信息infer:
https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#inferring-within-conditional-types:
实用程序类型库有一个类型https://github.com/piotrwitek/utility-types#valuestypet(以及许多其他类型)
例如
import { ValuesType } from 'utility-types';
type NumberArray = number[];
// Expect: number
type NumberItems = ValuesType<NumberArray>;
Run Code Online (Sandbox Code Playgroud)