Proposal: noAny Flag #53497
Labels
Needs Proposal
This issue needs a plan that clarifies the finer details of how it could be implemented.
Suggestion
An idea for TypeScript
Uh oh!
There was an error while loading. Please reload this page.
Proposal:
noAny
Flagπ Search Terms
noAny, no, any, flag, uknown
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
It would be nice to have a
noAny
flag that disallowed the use ofany
at all, including implicitly and explicitly. Instead, the much safer alternativeunknown
would be used. Many developers feel thatany
is far too permissive and should never be used.With this flag, the following would be the case:
instead of the current:
Functions that return
any
as part of the standard library (or other libraries) would either require a cast:Or, a much bigger ask, the compiler could automatically convert these to
unknown
.π Motivating Example
Consider a project manager that wants to write extremely type-safe code to avoid uncaught runtime errors; This is exactly what TypeScript is designed for. However, he finds his junior developers littering their code with any and making it extremely unsafe. In this case, a
noAny
flag could be used to prevent these mistakes.Additionally, it'd be a nice check in general for any developers to have; It is often considered good practice to avoid
any
as much as possible, and everything possible with any is possible in a more safe way usingunknown
:The new version would disallow the first example because it is not type-safe, and require the second be used instead.
instanceof
andin
checks can also be used to narrowunknown
. Code written under this flag may be more verbose or lengthy, but it will be safer and cause less runtime errors.π» Use Cases
This could be used for any project/developer that prefers stricter type checking, which may of us, as TypeScript developers, tend to prefer.
The
any
type feels like exactly what TypeScript's creation was trying to prevent, and yet there is currently no way to disable it entirely (there is an eslint rule for it, but it is not a part of the TypeScript compiler options itself and even then can't enforce function calls that returnany
); Only implicitany
s can be disabled, and even then they can be missed by being returns from function calls someone else wrote:file1.ts:
file2.ts (with
noImplicitAny
enabled):Overall, this seems like a practical feature that would be highly desirable for many developers that prefer type-checking to be as strict as possible.
The text was updated successfully, but these errors were encountered: