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

enable TypeScript strict checks #10417

Open
paul-marechal opened this issue Nov 15, 2021 · 2 comments
Open

enable TypeScript strict checks #10417

paul-marechal opened this issue Nov 15, 2021 · 2 comments
Labels
quality issues related to code and application quality

Comments

@paul-marechal
Copy link
Member

We currently only have strictNullChecks enabled, which is the least we can do when using TypeScript. It already guards against assigning null or undefined to objects:

interface Foo {
  a: string
  b: number
}

const a: Foo = null; // Error thanks to strictNullChecks.

But there are other checks that are currently not enabled and that let some potential issues slip into the code base, e.g.:

interface Bar {
  method(a: Meh | number): string
}

class Baz implements Bar {
  // Here TypeScript should fail but doesn't because strictFunctionTypes is not enabled.
  method(a: string): string {
     return a;
  }
}

const bar: Bar = new Baz();
const result: string = bar.method({ a: 'meh', b: 2}); // `result` isn't a string here, but TypeScript doesn't know.

There's also strictPropertyInitialization that tries to enforce proper initialization of class fields. If not done at construction time, then the field must be declared as potentially undefined, because it will be undefined until some later point in time. This check has to be the most annoying one in our code base as we are using Inversify's property injectors:

@injectable()
class Qux {
  @inject(Something)
  protected something: Something; // TypeScript will complain here because not set at construction time.
}

This issue can be avoided by telling TypeScript to ignore checking those fields by adding a !:

@injectable()
class Qux {
  @inject(Something)
  protected something!: Something; // TypeScript is happy.
}

Enabling this check will incur a LOT of changes but going forward it will help catch mistakes where a field might be left undefined but is not typed as such.

@paul-marechal paul-marechal added the quality issues related to code and application quality label Nov 15, 2021
@tsmaeder
Copy link
Contributor

Is that list of checks you're proposing final or are there others that might get turned on?

@paul-marechal
Copy link
Member Author

I want to run tsc --strict which turns on every strict check listed here: https://www.typescriptlang.org/docs/handbook/compiler-options.html#compiler-options

flag type default
--strict boolean false
Enable all strict type-checking options.
--strictBindCallApply boolean true if strict, false otherwise.
Check that the arguments for bind, call, and apply methods match the original function.
--strictFunctionTypes boolean true if strict, false otherwise.
When assigning functions, check to ensure parameters and the return values are subtype-compatible.
--strictNullChecks boolean true if strict, false otherwise.
When type checking, take into account null and undefined.
--strictPropertyInitialization boolean true if strict, false otherwise.
Check for class properties that are declared but not set in the constructor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
quality issues related to code and application quality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants