noThirdPartyAny to flag anys that come from third-party type declarations #45605
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Suggestion
π Search Terms
Related:
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
I'd like to add a new flag, perhaps
noThirdPartyAny
, that makes it an error for a symbol to have anany
type if thatany
comes from third-party code, e.g. from@types
or fromlib/dom.d.ts
. (Just like withnoImplicitAny
, it would be fine if you wanted to explicitly give that symbol anany
.)π Motivating Example
TypeScript with
noImplicitAny
requires you to provide type annotations on all symbols that would otherwise implicitly have anany
type. You'd think that this would only leave you with explicitany
s, i.e. a symbol can only have anany
type if you write the wordany
somewhere.But that's not quite true because type declarations files (
@types
) can includeany
types as well.Maybe the most common example is
JSON.parse
(alsoResponse.prototype.json()
):I'm using
--strict
and I never wroteany
, but the type ofb
is stillany
and it leads to a runtime error that TS misses.Here's a more intricate React example that tripped me up this week:
This works correctly. The
e
parameter in the callback has a type ofReact.ChangeEvent<HTMLInputElement>
, which is exactly right.Now I decide to memoize the handler using
React.useCallback
, copy-pasting my callback:Again, this works (at runtime) and it passes the type checker. But it's actually a looming disaster because
e
now has anany
type! This comes from the React typings:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/07e9b3d9d301b7d17390c16816c02cdf92706ecb/types/react/index.d.ts#L1099-L1107
This use of
any
is a known issue (DefinitelyTyped/DefinitelyTyped#52873) that the maintainers prefer not to fix until the next major version of React comes out. That's fine, but it makesuseCallback
veryany
-prone.I've found that
any
s that come from@types
and the built-in libraries are quite common and always surprising. They letany
s creep into your code even when you think you're being strict. I'd like some way to have TypeScript help me fight back against these implicitany
s.π» Use Cases
See above.
noImplicitAny
solves this forany
s that are truly implicit. I'm proposing a new flag that would extend this toany
symbols that do appear explicitly in type declarations, just not ones that I wrote.You can use declaration merging to eliminate these on a case-by-case basis (see blog post) but this feels like playing whack-a-mole. The new
--useUnknownInCatchVariables
flag is another tool in this game ofany
whack-a-mole.The best way to spot these
any
leaks is with ts-coverage (https://github.com/plantain-00/type-coverage), which reports every symbol in your code that has anany
type, wherever it came from. The downside is that this exists outside the TypeScript toolchain, so I tend to only run it every once in a while, whereas type errors always get fixed immediately.The text was updated successfully, but these errors were encountered: