strictNullCheck Errors caused by accessing array attributes like .pop
in a while loop #23403
Description
Edit by @DanielRosenwasser: This issue is about JavaScript files.
TypeScript Version: 2.9.0-dev.20180412
Search Terms: .pop, strictNullChecks while(), loop, array, possibly, undefined
Code
const queue = [
() => { console.log("hello") }
];
while (queue.length) {
queue.pop()();
}
Expected behavior:
That because the while loop is checking the length of an iterable is first > 0, that array methods are callable and valid with out errors.
Actual behavior:
Error message: Cannot invoke an object which is possibly 'undefined'.
Playground Link:
https://www.typescriptlang.org/play/#src=const%20queue%20%3D%20%5B%0D%0A%20%20%20%20()%20%3D%3E%20%7B%20console.log(%22hello%22)%20%7D%0D%0A%5D%3B%0D%0A%0D%0Awhile%20(queue.length)%20%7B%0D%0A%20%20%20%20queue.pop()()%3B%0D%0A%7D
Relevant Information
This is a pretty common practice in webpack to use a queue to perform breadth first search traversals of our dependency graph in the fastest means possible. So it would be nice to not have to @ts-ignore
this line
Activity