You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TypeScript doesn't indicate that find has unique functionality among the Array.prototype methods in that it does not skip holes in an array. This seems to be intended behavior from what I have seen.
π Expected behavior
TypeScript should mark that e could be of type Foo or undefined.
It doesn't make sense in my opinion to type arr as an Array<Foo | undefined> if it is known that you will never explicitly insert undefined into the array. Other javascript iterator functions like forEach will only go through the single defined element of arr, so the line arr.forEach((e) => e.bar ===1) would not throw a runtime exception. However, typing the array as Array<Foo | undefined> would result in a TypeScript error on that forEach statement.
The text was updated successfully, but these errors were encountered:
TypeScript doesn't acknowledge the existence of sparse arrays; this particular function is only one of many which can get you into trouble. e.g. given the arr above, you could write for (const e of [...arr]) { e; } and have the same problem.
Bug Report
π Search Terms
Array, find, holes, sparse array
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
TypeScript doesn't indicate that find has unique functionality among the Array.prototype methods in that it does not skip holes in an array. This seems to be intended behavior from what I have seen.
π Expected behavior
TypeScript should mark that
e
could be of typeFoo
orundefined
.It doesn't make sense in my opinion to type
arr
as anArray<Foo | undefined>
if it is known that you will never explicitly insert undefined into the array. Other javascript iterator functions likeforEach
will only go through the single defined element ofarr
, so the linearr.forEach((e) => e.bar ===1)
would not throw a runtime exception. However, typing the array asArray<Foo | undefined>
would result in a TypeScript error on that forEach statement.The text was updated successfully, but these errors were encountered: