Repository navigation
Suggestion: noInferredAny #39633
Copy link
Copy link
Open
Labels
Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.This issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScriptAn idea for TypeScript
Description
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.tstypes default generic arguments toany(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(); // okChecklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.This issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScriptAn idea for TypeScript
Activity
RyanCavanaugh commentedon Jul 17, 2020
So consider something like
Is this an "inferred" any because
k.mhas type any? If so, how does TypeScript perform an infinite examination of all potential properties (it can't)? If not, is the feature working? Does the access occur onk.mcause the error instead? Or on property access ofk.m?Thinking about this in terms of only binding an inference to a top-level binding just misses a lot of cases. If you keep poking at this hornet's nest you end up with the proposed
--strictAny, #24737. We can revisit that one again later, or if you have more concrete proposal here for exactly what would count as an error and a demonstration of how that would work beyond basic examples, that'd be interesting toomprobst commentedon Jul 20, 2020
stefee commentedon Sep 5, 2021
For anyone finding this now and wondering if there is a solution, you can use typescript-eslint with the following rules:
Links to the documentation for these rules can be found here: https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin#supported-rules
You can also add these rules, plus some others, using the
recommended-requiring-type-checkingpreset:You must also add this to your eslint config to allow the eslint plugin find your ts project configuration which enables it to do type-checking:
Here's the full docs for the parser config: https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/parser#configuration
osdiab commentedon Dec 19, 2024
Stef (@stefee) 's solution from a few years ago works, though I would love if this were built into TypeScript instead of needing to use
typescript-eslintfor it, since it is extremely slow and makes many spurious errors in the editor when files change, like this: microsoft/vscode-eslint#1321I don't really care if
anyis being used in library code, but if in my own code I could have TypeScript throw an error basically identically to what Typescript ESLint does with the above set of rules; then that would be perfect for my use cases—so if that can act as a concrete proposal of what exactly counts as an error, and a demonstration of how it would work beyond specific examples, then I hope that remains interesting!