【发布时间】:2019-12-13 15:03:33
【问题描述】:
我有这段代码
let hostel : HostelType;
hostels.forEach( (r) => {
const i = r.identifier.findIndex((_identifier: any) => _identifier.id === '433456');
hostel = hostels[i];
});
hostel.serviceLevel.value = 'P';
但我有一个编译错误:
Variable 'hostel' is used before being assigned.
【问题讨论】:
-
你的可变宿舍没有任何价值
-
您将
hostel声明为HostelType类型,但您没有对其进行初始化。因此,如果 forEach 没有价值,那么hostel = hostels[i]将不会完成。所以你然后尝试访问一个空对象的serviceLevel。
标签: javascript node.js typescript