小编Ale*_*oti的帖子

获取混合类型数组每个索引的元素类型

我有一个混合数组,如:

const array = [false, 1, '', class T {}];
Run Code Online (Sandbox Code Playgroud)

谁的类型是:

type arrayType = typeof array; // (string | number | boolean | typeof T) []
Run Code Online (Sandbox Code Playgroud)

并且任何索引中的对象类型是:

string | number | boolean | typeof T
Run Code Online (Sandbox Code Playgroud)

如何从特定索引获取对象的类型,如下所示,而不是类型的联合?

const a = array [0] // should be boolean
const b = array [1] // should be number
const c = array [2] // should be string
const d = array [3] // should be typeof T
Run Code Online (Sandbox Code Playgroud)

TS游乐场

typescript

4
推荐指数
1
解决办法
62
查看次数

标签 统计

typescript ×1