In my generator function, I am yielding an array containing values of type [number, object]
. By using a for...of
loop to iterate over the function and destructuring the values with for (const [k, v] of iterator())
, I noticed that the type of v
is number | object
. This was unexpected as I had specified the type as object
in the yield
statement of the generator function.
To demonstrate this issue, I have created a TypeScript REPL example which can be found here.
The issue arises in line number 22, where the TypeScript compiler raises an error regarding accessing a key that may not exist post-destructuring.
I am puzzled by why TypeScript assumes that the variables obtained through destructuring could be either of the specified types?