我在打字稿游乐场创建了一个typescript snippet
function test(items: number[]) {
let item;
while((item = items.shift()) !== undefined) {
console.log(item); // inferred as number
// const dums = [ 1, 2];
// dums.forEach(() => {
// console.log(item);
// });
}
}
function test2(items: number[]) {
let item;
while((item = items.shift()) !== undefined) {
console.log(item); //inferred as number
const dums = [ 1, 2];
dums.forEach(() => {
console.log(item); // typescript flag it as Variable 'item' implicitly has an 'any' type.
});
}
}
可以在开头将项目对象的类型推断为数字,而在函数闭包中推断类型停止?这是错误还是功能?