Could they be removed?
Could they be removed?
I highly recommend using TypeScript's strict null checks mode as it makes undefined and null their own distinct types and requires that you explicitly state when a field or argument is nullable. The programming experience is somewhat similar to working with 'optional' types in languages like Scala or Swift. See the 'nullable types' section of this document for more info: https://www.typescriptlang.org/docs/handbook/advanced-types....
Like this, pick your flavor:
https://doc.rust-lang.org/std/option/enum.Option.html
https://docs.oracle.com/javase/8/docs/api/java/util/Optional...
https://docs.microsoft.com/en-us/dotnet/api/system.nullable-...
https://wiki.haskell.org/Maybe
If you don't want to follow links: Instead of having nulls, have a type which is either a discriminated union (not possible in JS) or is a (isPopulated, value) pair, and accessing the value is an exception if isPopulated=false.
Granted, that would be an extremely breaking change for a JS-compatible language, which would make it in fact incompatible. But answering the question, that's it.
Exception handling and null checks are both conditional, so there is the potential to replace checking for null with exceptions. Plus, removing the null checks (is an object still an object when most of its properties are null?) could be fixed with better use of inheritance. And the '?' suffix.
This is not the first language Anders has been involved in developing, I'm sure remember him talking about his dislike for null somewhere (I could be wrong). Typescript is both an abstraction and adds features to Javascript. So TypeScript has the opportunity to remove such idioms. Scala and Java are very different languages despite running the same underneath.
The question at the end of the post was more hypothetical. Could this be done or is the concept of null is so ingrained that it can't be removed? (When Functional programming manages null.) I'm just interested, especially when null is known as the "Billion Dollar Mistake" - that's ignoring inflation.
Probably a convoluted example, however there are distinctions that can be used between the two. Similar to how I've used null + boolean as a tristate with default, true, false where null is default. Undefinied or uninitialized is useful beyond simply null.