Should I use the noImplicitAny TypeScript compiler flag?
noImplicitAny
What the compiler options do is basically convert TypeScript from optional type language to mandatory type verification language.
This makes TypeScript a little further away from the JavaScript superset because it is simple:
function logMe(x) { (x); } // error TS7006: Parameter 'x' implicitly has an 'any' type.
There will also be an error - you must declare it clearlyx
The type ofany
:
function logMe(x: any) { (x); } // OK
This means that if you are migrating your existing JS code base to TS, you have to do something more complicated than changing the file extension. This also means that when writing code, you need to pay more attention to types, and if you don't specify types, the compiler will always "complain".
Explicitly declare
Because it is explicitly declared in actual situationsany
It is considered bad practice, so early in the development process, you need to assign the correct type. Without an explicit declaration, this could mean "I'm too lazy to comment on the type here correctly".
Whether this is good or bad is very controversial, and the community seems to have differences on this issue. Here are some of the industry's leading TypeScript projects and whether they are usednoImplicitAny
Compiler flags:
Project | Uses noImplicitAny |
---|---|
Angular | YES |
RxJS | YES |
VSCode | NO |
NO |
Next is my point: We use TypeScript because the types provide meaningful additional information that can be used as documentation and catch errors early on. If you want to enjoy this benefit in your project's code, you shouldn't just add types somewhere - add them anywhere and you're done.
Otherwise you may think as follows:
"Well, should I add the types here? I'm a little lazy, but that's good, but I have other work to do... "Let's do it tomorrow."
Therefore, my suggestion is tonoImplicitAny
Set astrue
。
The above is the detailed content of TypeScript's actual combat analysis using noImplicitAny. For more information about TypeScript's use of noImplicitAny, please follow my other related articles!