Reactjs 为什么可以';我不能在React中使用迭代器或Map.get()?
这里是React的新手,遇到Map对象的问题,无论出于何种原因,TypeScript/React不喜欢我尝试使用Map.get()或尝试像这样迭代:Reactjs 为什么可以';我不能在React中使用迭代器或Map.get()?,reactjs,typescript,Reactjs,Typescript,这里是React的新手,遇到Map对象的问题,无论出于何种原因,TypeScript/React不喜欢我尝试使用Map.get()或尝试像这样迭代: interface IProps { schedule: Map<string, Schedule> } const StoreHours = ({ schedule }: IProps) => { for(const key in schedule) { console.log(key);
interface IProps {
schedule: Map<string, Schedule>
}
const StoreHours = ({ schedule }: IProps) => {
for(const key in schedule) {
console.log(key);
// this line gives me an error 'Uncaught TypeError: schedule.get is not a function'
//console.log(schedule.get(key));
}
for (let [key, value] of schedule) {
//schedule is not iterable if I uncomment this
//console.log(value);
}
return (<div>test</div>);
}
export default StoreHours;
接口IProps{
附表:地图
}
const StoreHours=({schedule}:IProps)=>{
用于(施工索引表){
控制台日志(键);
//这一行给了我一个错误“UncaughtTypeError:schedule.get不是函数”
//console.log(schedule.get(key));
}
用于(让[键,值]作为进度表){
//如果我取消对此的注释,则日程安排不可修改
//console.log(值);
}
返回(测试);
}
导出默认存储时间;
如果我取消注释,我会得到错误:“uncaughttypeerror:schedule.get不是一个函数”-或者如果我使用for…of-有什么原因我不能使用映射吗?多谢各位
…有什么理由我不能使用地图吗
当我们在tsconfig.json
文件中瞄准es5
时,我们不能在Map
上使用of
进行迭代
TypeScript是JavaScript的语法超集,而不是功能超集。当语法和功能相交时,会有一点灰色区域当以ES5为目标时,TypeScript假定只有基于数字索引的对象(ArrayLike)可以使用for向下发射。。。因为这些只是ES5保证支持的功能结构。[重点补充]
这里有三种选择。对于在web浏览器中运行的应用程序,最后一个是最简单的
- 目标
es6
- 使用巴别塔
- 在
tsconfig.json
另请参见:您是否会使用
for..of
在地图上迭代?我同意@AlexanderStaroselsky-我尝试得太早了-添加到帖子中(我尝试这种方式时刚刚决定发布)你能分享一下时间表
是如何创建并传递到StoreHours
的吗?