Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用TypeScript编译器API提取数组的类型_Typescript_Typescript Compiler Api - Fatal编程技术网

如何使用TypeScript编译器API提取数组的类型

如何使用TypeScript编译器API提取数组的类型,typescript,typescript-compiler-api,Typescript,Typescript Compiler Api,如何使用TypeScript编译器API提取数组的类型?例如,鉴于此来源: let a: string[] = [] 如何从获取类型string[]转变为仅获取string 这很容易,但我还没有弄清楚如何用原始TS编译器API复制它 似乎我需要使用checker.getTypeArguments(),但它需要一个ts.TypeReference类型,我不知道如何创建它。一个数组类型,例如string[]将采用以下形式: Array<string> 要检查一个类型是否是数组类型,我

如何使用TypeScript编译器API提取数组的类型?例如,鉴于此来源:

let a: string[] = []
如何从获取类型
string[]
转变为仅获取
string

这很容易,但我还没有弄清楚如何用原始TS编译器API复制它


似乎我需要使用checker.getTypeArguments(),但它需要一个
ts.TypeReference
类型,我不知道如何创建它。

一个数组类型,例如
string[]
将采用以下形式:

Array<string>

要检查一个类型是否是数组类型,我通常只检查
array
的类型符号名称,以及它是否有单个类型参数。

这是否回答了您的问题?我问的是元级别的TypeScript编译器API。标题也相应地更新了。另请看《谢谢大卫》,让我困惑的是演员阵容。没有别的办法吗?你怎么知道的?是否期望API用户像这样直观地使用,或者您是通过gitter等与核心团队交谈了解到这一点的。?
const typeArgs = typeChecker.getTypeArguments(arrayType as ts.TypeReference);
const elementType = typeArgs[0];