【发布时间】:2021-06-30 22:39:26
【问题描述】:
public RowData: PricingOrder[] | Notification[] | ActionLog[] | string[];
如果 RowData 是 string[] 类型 (Ex-RowData = ['cat','dog','anyother'];)
if(RowData === string[] type ){
//perform some string array related operation
} else {
//perform custom class array related operation
}
【问题讨论】:
-
这能回答你的问题吗? Test for array of string type in TypeScript
-
使用
Array#some(或Array#every)和typeof的最简单的实现是:if (RowData.some(item => typeof item === 'string')) { /* string type */ } else { /* other type */ } -
@MichaelD 我不想遍历数组的内容。有没有其他直接变量类型的技巧?
-
@sumit-sharma 是否有可能有这样的数组
[1,"a"]?我的意思是数字和字符串的组合? -
不,它总是只有数组中的字符串值。
标签: javascript angular typescript