Search Terms
noImplicitAny inference any noInferredAny
Suggestion
Symbols typed as any reduce the type safety of a program. That's fair if the programmer intended to use any. However it's relatively easy to end up with symbols typed any through type inference chains that are not obvious:
- 3rd party libraries might return
any
- complex type expressions might infer to any
- some
.d.ts types default generic arguments to any (e.g. Set)
In all these situations, a programmer might write const foo = something(); and expect foo to have a reasonable inferred type. foo being inferred to any is easy to miss in such code, both while editing and while reviewing code.
Proposal: add a compiler option noInferredAny that flags symbols whose type is any and that do not have an explicit type annotation.
Use Cases
- better type safety for programs
- detect weak typings in your dependencies
- more
Examples
const foo1 = returnsAny(); // error
const foo2: any = returnsAny(); // ok
const foo3: string = returnsAny(); // probably ok, programmer gave an explicit type? could also require an explicit cast.
const foo4 = returnsAny() as string; // ok
const {foo5}: {foo5: string} = returnsAny(); // ok
Checklist
My suggestion meets these guidelines:
Search Terms
noImplicitAny inference any noInferredAny
Suggestion
Symbols typed as
anyreduce the type safety of a program. That's fair if the programmer intended to useany. However it's relatively easy to end up with symbols typedanythrough type inference chains that are not obvious:any.d.tstypes default generic arguments toany(e.g.Set)In all these situations, a programmer might write
const foo = something();and expectfooto have a reasonable inferred type.foobeing inferred toanyis easy to miss in such code, both while editing and while reviewing code.Proposal: add a compiler option
noInferredAnythat flags symbols whose type is any and that do not have an explicit type annotation.Use Cases
Examples
Checklist
My suggestion meets these guidelines: