0014 - Adopt Typescript Strict flag
ID: | ADR-0014 |
---|---|
Status: | IN PROGRESS |
Published: | 2022-09-02 |
Context and Problem Statement
For compatibility reasons since the codebase was originally written in JavaScript we have never been able to use the TypeScript strict flag. This has lead to several issues with bugs and resulted in a lower code quality.
TypeScript Strict
The
https://www.typescriptlang.org/tsconfig#strictstrict
flag enables a wide range of type checking behavior that results in stronger guarantees of program correctness. Turning this on is equivalent to enabling all of the strict mode family options.
Of particular note is the Strict Null Checks. By default TypeScript will ignore false
,
null
and undefined
allowing them to be used for any types. When strictNullChecks
are enabled
they must be explicitly allowed in the type definition.
Typescript strict mode plugin
The Typescript strict mode plugin is a TypeScript plugin that allows you to progressively turn on strict mode on a file by file basis. This will allow us to progressively update our codebase to support strict mode without requiring a significant initial engineering effort.
Considered Options
- Keep strict disabled - We will continue to run into null issues, and code quality will be lower than it could be.
- Enable strict flag immediately - Turn on the strict flag immediately and assign an engineer to resolve all errors caused by it. This will take a long time, and result in a multitude of conflicts.
- Progressively enable strict flag - Use the TypeScript plugin to migrate files one by one.
Decision Outcome
Chosen option: Progressively enable strict flag.
Positive Consequences
- We can immediately start using the strict flag.
- Lower initial developer effort.
Negative Consequences
- Migration will be slower.
- We will continue to have parts of the code with null issues.
- Might lead to false security if a file is strict but uses non-strict dependencies.
- Few best practices.
Files to be updated
- libs/common: 310
- models: 174
- services: 40
- abstractions: 45
- libs/angular: 64
- libs/node: 11
- libs/electron: 10
- apps: 309
- web: 163
- browser: 80
- desktop: 30
- cli: 36
Plan
There are several potential pitfalls related to progressively enabling strict mode. Which will benefit from having a plan on how we should tackle the migration. Each category of files should have a proper example how how they should be migrated. And during the initial migration period a best effort should be made to be strict-compliant, but we should not prevent people from adding new files that are not strict. What we should prevent is people adding new non-strict files in the category that we are currently migrating.
Preparation
Enable the plugin and run update-strict-comments
which adds the //@ts-strict-ignore
comment to
all files which produce a strict error.
Transition
- Migrate
libs/common/models
. - Migrate
libs/common/services
andlibs/common/abstractions
. - Migrate remaining
libs/common
. - Migrate
libs/angular
. - Migrate apps. (And forbid anyone from adding new non-strict files)