【发布时间】:2021-06-21 15:35:00
【问题描述】:
我在检查联合类型数组、字符串和数字的长度时遇到问题。在 prod 模式下构建 Angular 项目时的问题示例代码:
ts 中的变量
public developerData: {
[key: string]: {
type: 'default';
data: string | number;
} | {
type: 'array',
data: Array < string | number | boolean >
}
} = {
'test': {
'type': 'array',
'data': ['1', '2', '3']
}
};
HTML 模板
<div *ngIf="developerData['test']['type']=='array' && developerData['test']['data'].length >= 0">
Welcome
</div>
产品构建错误
类型“字符串”上不存在属性“长度”|号码 | (字符串 | 号码 |布尔值)[]'。类型上不存在属性“长度” '数字'。
软件版本
-
角度:7.3.5
-
TypeScript:3.1.6
-
节点:10.16.3
-
NPM:6.9.0
【问题讨论】:
标签: html angular typescript union-types