Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn on TypeScript 'strict' compiler option? #22

Open
jasongrout opened this issue Jul 6, 2017 · 3 comments
Open

Turn on TypeScript 'strict' compiler option? #22

jasongrout opened this issue Jul 6, 2017 · 3 comments

Comments

@jasongrout
Copy link
Contributor

jasongrout commented Jul 6, 2017

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.

@jasongrout jasongrout changed the title Turn on TypeScript 'strict' compiler option Turn on TypeScript 'strict' compiler option? Jul 6, 2017
@dpvc
Copy link
Member

dpvc commented Jul 6, 2017

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?

@jasongrout
Copy link
Contributor Author

jasongrout commented Jul 6, 2017

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

@jasongrout
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants