Re:Bad TypeScript Habits
by Jasper_ on Tuesday February 02, 2021 @09:58PM
(#26010068)
Why does Array.prototype.find() return undefined and not null if it didn't find a value? According to your logic, the sentinel to indicate "this array doesn't have an element that matches the predicate" should be null.
[ Parent
| on HN ]
Re:Bad TypeScript Habits
by chrismorgan on Wednesday February 03, 2021 @02:07AM
(#26011327)
I don’t know. I would guess that that return type involved a slightly heated discussion before undefined won over null so that you can distinguish it returning an element that is null.
(This is a fundamental problem of nullable types which is solved by algebraic data types; in Rust, the equivalent method would return None for “no value found”, or Some(None) if it found a None value.)