打字稿类型推断正确启动然后停止

时间:2020-10-20 17:53:25

标签: typescript

我在打字稿游乐场创建了一个typescript snippet

enter image description here

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.
    });
  }
}

可以在开头将项目对象的类型推断为数字,而在函数闭包中推断类型停止?这是错误还是功能?

0 个答案:

没有答案