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
{{ message }}
Repository navigation
Array.prototype.find not typing input as possibly undefinedΒ #49913
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.
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.
Activity
RyanCavanaugh commentedon Jul 14, 2022
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
arrabove, you could writefor (const e of [...arr]) { e; }and have the same problem.ChrisMcD1 commentedon Jul 14, 2022
Oh, okay! Thank you for informing me.