我试图在地图的返回中添加元组,但得到错误,"use strict"const row = ['true', '12', 'apple']type cleanRowType = [boolean, number, string]const cleanRow: cleanRowType = row.map( (item, i) => i === 2 ? item : i === 1 ? +item : i === 0 ? Boolean(item) : '')console.log('cleanRow', cleanRow)https://stackblitz.com/edit/typescript-6dbd8k
1 回答
慕运维8079593
TA贡献1876条经验 获得超5个赞
Typescript不够聪明,无法知道数组中每个元素的单独类型,它只知道每个元素都是。要解决此问题,请不要使用地图:boolean | number | string
const cleanRow: cleanRowType = [Boolean(row[0]), +row[1], row[2]];
添加回答
举报
0/150
提交
取消
