Enable more linter checks in tsconfig.json by default
See original GitHub issueđ Feature request
Command (mark with an x)
- new
- build
- serve
- test
- e2e
- generate
- add
- update
- lint
- extract-i18n
- run
- config
- help
- version
- doc
Description
⯠npx @angular/cli@12 new web-apps --create-application false
⯠cd web-apps && yarn ng g app admin-console --routing --style scss
Currently, base tsconfig.json only contains strict, noImplicitReturns and noFallthroughCasesInSwitch flags
{
"compileOnSave": false,
"compilerOptions": {
...
// all strict checks
"strict": true,
// Linter checks
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
...
}
}
Describe the solution youâd like
Enable new linter flags exposed by typescript in recent versions by defalt
{
"compileOnSave": false,
"compilerOptions": {
...
// all strict checks
"strict": true,
// Linter checks
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
// TS 4.1
"noUncheckedIndexedAccess": true,
// TS 4.2
"noPropertyAccessFromIndexSignature": true,
// TS 4.3
"noImplicitOverride": true,
// TS 4.4
"useUnknownInCatchVariables": true,
"exactOptionalPropertyTypes": true,
...
}
}
Relevant blog posts:
- Announcing TypeScript 4.1
--noUncheckedIndexedAccess
- Announcing TypeScript 4.2
--noPropertyAccessFromIndexSignature
- Announcing TypeScript 4.3
--noImplicitOverride
- Announcing TypeScript 4.4
--useUnknownInCatchVariables--exactOptionalPropertyTypes
Describe alternatives youâve considered
I just have to add these flags manually to tsconfig.json
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
TSConfig Reference - Docs on every TSConfig option
Setting the value to undefined will allow most JavaScript runtime checks for the ... will look for files starting at the same folder...
Read more >Understanding TypeScript Configuration Options
allowUmdGlobalAccessLinter Checks ... Install typescript and generate tsconfig.json : $ npm init ... For more description, you can see the docs.
Read more >How to use ESLint with TypeScript | Khalil Stemmler
ESLint is a JavaScript linter that enables you to enforce a set of ... In your project package.json , lets add a lint...
Read more >no-unused-variable is deprecated; but I'm not using this config ...
Since TypeScript 2.9. Please use the built-in compiler checks instead. But my tsconfig.json does not seem to use this. {Â ...
Read more >Linting in TypeScript using ESLint and Prettier - LogRocket Blog
This will generate a default TypeScript configuration file; Create a file called tsconfig.json at the root directory of your project and include ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

It is a good idea to consider updating our strictness with recent TypeScript versions. Although it is worth keeping in mind that
npx typescript --initdoes not enable any of these by default. Onlystrictis enabled by default. Angular can probably be a bit more opinionated here, but we should take care to not diverge too much where there isnât sufficient value to justify it.After discussion within the tooling team, our takeaway with these flags is (interested to get @mgechevâs opinion as well):
noUncheckedIndexedAccessThis is really useful for objects and really annoying for arrays. having
a[0]return anundefinedtype is awkward and particularly confusing to those who might be learning arrays for the first time. It also applies to array destructuring unless you type the object as a tuple[string, /* ... */]instead of an arraystring[]. Advanced users might understand the difference and be able to work around it, but this can easily trip up new devs.The main benefit of catching possibly undefined properties in objects only applies when using index types which is a bit of anti-pattern IMHO,
Mapobjects should be preferred where possible (or even aRecord<string, T | undefined>).As a result, we feel that the benefit for catching undefined properties in objects is outweighed by the awkwardness of using arrays and shouldnât be included by default.
noPropertyAccessFromIndexSignatureWe generally like this option as it is a pretty simple change which clarifies the user intent and catches possible typos without much friction. The error message is very straightforward and easily fixed. This should be included by default.
noImplicitOverrideThe
overridekeyword is really nice for catching accidental overrides that may not have been intentional. We think this is a good feature for the type system and very useful to have.There were some concerns about this breaking a lot of existing documentation and education content already out there, however most Angular APIs do not use inheritance, so not much is actually broken.
overrideis not required when implementing interfaces orabstractclasses, so things likengOnInit()are not affected by this option. As a result, users can define an Angular component, directive, service, etc. without running into a breakage. We couldnât think of any Angular APIs that expect users extend an override a method. Iâm sure some exist, but they are obscure enough to not be an issue for new devs getting started with Angular.Our takeaway is that this doesnât introduce much friction for new devs or existing educational content, so it is good to include by default.
useUnknownInCatchVariablesUsing
unknownincatchvariables is more reflective of what the type system actually knows about a thrown object, but is often more annoying to use in practice. Throwing non-Errorobjects is generally considered an anti-pattern and should be avoided. Enabling this would also likely break almost all existing educational content which catches an error.Our preference is for users to consider lint checks like
no-throw-literaland treating any caught objects asError. We think that would provide a better experience thanuseUnknownInCatchVariables.exactOptionalPropertyTypesThis option draws a clear distinction between
foo?: stringandfoo?: string | undefinedwhich can be very confusing to new users. In particular, One particularly awkward edge case is that you canât put astring | undefinedvalue into an optionalstringtype and the error message is very unhelpful in this regard.This is a very nuanced option and will easily trip up even advanced developers who arenât familiar with it. I think I saw somewhere that even the TypeScript team doesnât recommend this unless you have a good reason for it in your project, but Iâm not able to find that statement now. Our take is that this is too nuanced and specific, making it likely to cause more pain than it actually solves and shouldnât be included by default.
Of course, all of these options can be manually added with minimal effort, this is only talking about what should be included by default for
ng newprojects. In total, this means thatnoPropertyAccessFromIndexSignatureandnoImplicitOverrideshould be added tong new. We should also gate these so they are not included when--nostrictis provided, since the user wouldnât want them in that case anyways (and I think they both requirestrictNullChecksregardless).These should be added in the next major to reduce breakages of existing educative content (please please please, pin the versions of any tutorials or workshops you own specifically to avoid breakages from stuff like this). TS 4.3 should be the minimum by Angular v13, so it should be safe to include those two without worrying about older TypeScript versions that donât support these flags.
I would love to migrate existing projects with the same options, however there donât appear to be any existing codemods which do this. VSCode can fix all
noImplicitOverrideandnoPropertyAccessFromIndexSignatureerrors in a file, and both are pretty precise fixes, so migrating a project should be relatively easily, even if not done automatically. Weâll probably skip existing projects on this for now and let them migrate themselves.This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.