-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Suggestion: noInferredAny #39633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
So consider something like // Elsewhere
declare function fn<T = string>(): T extends "foo" ? { p: boolean } : { m: any };
const k = fn(); Is this an "inferred" any because 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 |
So consider something like
// Elsewheredeclare function fn<T = string>(): T extends "foo" ? { p: boolean } : { m: any };
const k = fn();
Is this an "inferred" any because k.m has type any?
I would say no: `k` is inferred to `{m: any}`, and `m` has been explicitly
declared to be `any`.
Now if you'd follow up with `const j = k.m;`, `j` would be an inferred any,
and you'd get an error.
I think you're right though, what I was thinking of sounds like a dupe
of #24737. Thanks for the pointer, that's an interesting read.
… |
For anyone finding this now and wondering if there is a solution, you can use typescript-eslint with the following rules: "rules": {
"@typescript-eslint/no-unsafe-argument": "error",
"@typescript-eslint/no-unsafe-assignment": "error",
"@typescript-eslint/no-unsafe-call": "error",
"@typescript-eslint/no-unsafe-member-access": "error",
"@typescript-eslint/no-unsafe-return": "error"
} 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 "extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
] 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: "parserOptions": {
"project": "./tsconfig.json"
}, Here's the full docs for the parser config: https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/parser#configuration |
@stefee 's solution from a few years ago works, though I would love if this were built into TypeScript instead of needing to use I don't really care if |
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 useany
. However it's relatively easy to end up with symbols typedany
through type inference chains that are not obvious:any
.d.ts
types default generic arguments toany
(e.g.Set
)In all these situations, a programmer might write
const foo = something();
and expectfoo
to have a reasonable inferred type.foo
being inferred toany
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
Examples
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: