MathJax-src
MathJax-src copied to clipboard
Turn on TypeScript 'strict' compiler option?
You might consider turning on the compiler 'strict' option (see https://www.typescriptlang.org/docs/handbook/compiler-options.html) in your tsconfig, which turns on a few options related to making the type checking more strict. For example, it turns on the strict null checking to help guard against errors involving null or undefined, etc. The nice thing about using 'strict' is that it will automatically updated in each TS release to be the recommended set of strict options. Alternatively, you might look at enabling individually each of the options 'strict' enables.
Thanks for the pointer. It looks like this turns on four things: noImplicitAny, noImplicitThis, alwaysStrict, and strictNullChecks. We already have noImplicitAny enabled, and I don't think noImplicitThis or alwaysStrict would cause us any problems. But I'm not certain about strictNullChecks. I understand the desire to be careful about this, but I suspect it would take some work to make that compile cleanly for us (we do currently use the fact that null is allowed for any type at the moment), if I read this correctly.
I'm curious how this is supposed to be handled. Do you have to use a lot of type | null expressions when dealing with things that could be null (e.g., option lists, or child pointers, and so on)? or is there some better way to manage that?
I think so - I don't have a ton of experience with it yet. If you already have type aliases set up, you can add | null. After you check for null, Typescript can infer the type is not null, or you can explicitly declare it so with the ! suffix to the type (e.g., string! declares a string | null as nonnullable).
See also "Nullable types" in https://www.typescriptlang.org/docs/handbook/advanced-types.html
The strict null checking probably does take the most work to satisfy. We haven't turned it on for some of our codebases yet either.