Non-nullable types for TypeScript
github.com
github.com
Also, "strictNullChecks" is kind of optional security, which is almost always a bad idea in the long run.
Maybe they did this for backwards compatibility? Are there any plans to make "strictNullChecks" the default in the future?
I hope the --noImplicitAny option will become default.
https://github.com/Microsoft/TypeScript/wiki/What%27s-new-in...
That should make it simple to treat ts more strict than js.
How does the ability to compile plain JS alter its goal of being a JS superset?
Not sure why the default matters at all. The idea behind TS is to be able to gradually turn up the strictness dial as you migrate from JS
In this case, too, there's a maintenance burden in that probably a bunch of type definitions files may need updating before this new strictness is usable in some projects. So there's a direct backward compatibility issue with existing Typescript code that will need to be addressed for this new strictness.
One of the proposals I've seen in the GitHub issues is that Typescript should at least offer a "Maximum Strictness" option that turns on all of the optional strictness checks together, which would be great for fresh projects, with the assumption that new versions of Typescript present some maintenance requirements for those projects. I would love to see that added to Typescript.
Of course JavaScript itself does not have a notion of nullable types, so touching any native API interfaces naturally leaves you with a bunch of nullable types where you may well know the value will never be null, so you are left trying to decide where to do nothing, where to add an assertion, and where to add a conditional branch. This ambiguity comes through the Closure API in many places that it would be nice to have a non-nullable type guarantee, but I suppose the library has evolved over time and further must be adaptable to a range of coding styles.
So what's the point then?
It would be saner if everything was non null by default. There'd be no need for a ! then and no ambiguity for beginners around whether something is nullable or not. No downside for non-beginners either unless they're in the habit of using more nullable parameters/vars than non-nullable, which doesn't seem like a particularly good idea...
Agreed on the defaults, although you then have the problem of reconciling with the native JS type system. I'm unclear on what this PR does to address that for TS - in strict null check mode, can an e.g. Node be null by default? If so, does the native "Node" object then become "Node|null"? At some level this has to be addressed.
Native objects can never be null. If its an object, it already isnt null. Functions,property accessors etc. can return Node or null or undefined, which can be modelled accordingly on a per-function basis
1) It's written in Java which makes it a hassle to use in the typical front-end project setup.
2) The documentation is just atrocious.
If a native API really never returns null it can be declared as such in Closure's externs (equivalent of a header file). If it can return null then doing a routine null check will automatically add the non-nullable attribute when the check succeeds, no cast required.
This is interesting to me because it is:
1) Unnecessarily conservative.
let x = { y: { z: intOrNull() } };
if (x && x.y && x.y.z) {
const z = x.y.z;
x.y = null;
// z is of type "number?" when it could be "number"
}
2) Not sound w.r.t. aliasing. let x = { y: { z: intOrNull() } };
let x_ = x;
if (x && x.y && x.y.z) {
x_.y.z = null;
const z = x.y.z;
// z is of type "number" with a value of "null"
}The issue: https://github.com/Microsoft/TypeScript/issues/5822
[0] as someone mostly uninterested in TS
> (...) there's already the let keyword available for those who want saner scoping rules. We should definitely update the docs to use let everywhere, though.
[1]: https://github.com/Microsoft/TypeScript/issues/5822#issuecom...