Too bad that "strictNullChecks" is not the default behaviour. Because this is the only really sane default, and is one the the things I loved so much in strongly types languages such as OCaml, F# or Haskell. It reduces so make case distinctions (and forgotten if/switch checks) to the only places where these are really used.
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?
Since this is a brand new feature that isn't even accepted into the language yet, it obviously can't be made default. If they decide to make it the default behavior, there would need to be a period where the old bahavior is depreciated and triggers warnings, but not errors.
I hope the --noImplicitAny option will become default.
One of the most touted features is "your JavaScript is already valid TypeScript" (or pretty close), so I think they will keep the defaults so that JS developers can just change the .js to .ts and be up and running with as little friction as possible.
Because it can compile plain JS using the rules present under plain JS, such as nullable and undefined-able types by default, regardless of the rules it's using to compile TS.
Typescript leans in the direction for increasing strictness one flag at a time in a project, which is particularly fair if you're migrating a legacy project but not always the most sane option for new projects.
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.
Languages like Haskell can get away with it because everything is statically compiled. But what would this look like for an environment like JS, that dynamically links against the user's browser? The user's browser may be older or newer, and expose different properties. Should every browser API be potentially nullable?
The Google Closure type system is much saner on this front already. A variable of a declared type cannot be undefined. Nullable and non-nullable types are distinct: primitives are non-nullable by default, with an option to make them nullable (?string, ?number, etc.), and types deriving from Object are nullable by default, with an option to make them non-nullable (!Object, !MyType, etc.)
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.
> 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
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.
You can flip the default. Back when I was using Closure Compiler, it was the expected convention on my team to mark most of your type annotations non-nullable.
you mean value types rather than primitives right? (Strings are objects)
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
I'm really impressed with how quickly the Typescript team is adding new features - it feels like every time I check in on them they have something new in the works that I really care about. Great work!
> A type guard for a dotted name has no effect if any part of the dotted name is the target of an assignment within the guarded code. For example, a type guard for x.y.z will have no effect if the guarded code contains any assignments to x, x.y, or x.y.z.
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"
}
Really glad to see this. I encountered the use-before-declaration "feature" of TS, and I just assumed it was a bug, which I filed. I was flabbergasted when I was told it was by design, to match JavaScript.
Funny how the conclusion is basically "Don't use `var`, use `let` instead" - even though almost all documentation/examples exclusively use the `var` keyword!
> (...) 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.
I can only disagree and that thread gave me an excellent impression of the TS team[0]. They cleanly noted that this was expected behaviour, the reasoning behind it (which calls back to one of the underlying roles of typescript) and how to mitigate it. Can't say the same of your vote-spamming of every comment though.
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?