Compiler option to switch lib .d.ts any
s to unknown
#60899
Labels
Needs Proposal
This issue needs a plan that clarifies the finer details of how it could be implemented.
Suggestion
An idea for TypeScript
🔍 Search Terms
compiler option flag lib.d.ts noImplicitAny unknown any
✅ Viability Checklist
⭐ Suggestion
A few built-in (
lib*.d.ts
) types includeany
, even thoughunknown
would be safer. Most notably:JSON.parse()
:TypeScript/src/lib/es5.d.ts
Line 1144 in 56a0825
.json()
: https://github.com/microsoft/TypeScript/blob/56a08250f3516b3f5bc120d6c7ab4450a9a69352/src/lib/dom.generated.d.ts#L19416Storage
: https://github.com/microsoft/TypeScript/blob/56a08250f3516b3f5bc120d6c7ab4450a9a69352/src/lib/dom.generated.d.ts#L22262These are kept as
any
for legacy support reasons: it would be a massive breaking change to restrict them tounknown
. But, for newer projects and those that don't rely on thoseany
s, switching them tounknown
would be much safer long-term.Proposal: could we have a compiler option to switch the lib definition
any
s tounknown
s? Maybe,strictLibDefinitions
?useUnknownInLibDefinitions
? (I'm not convinced of those specific names)📃 Motivating Example
Using this compiler option will prevent many of the common
any
s from sneaking into projects that use built-ins such asJSON.parse
andResponse
.For example, this code does not have a type error by default, but would with the new compiler option enabled:
💻 Use Cases
I'm not sure that this could even be enabled with
strict
anytime soon. It might introduce a lot of type errors in even many projects that are already in strict mode but happen to useJSON.parse
et al.This is not the same as #27265. That issue tracks a flag to handle implicit
any
s differently. This is for a flag to change the definitions in TypeScript's built-in.d.ts
files.I don't think this is the same as #26188. Per #60899 (comment): I'd interpreted that one as suggesting making the change always - with varying levels of compiler-option-orientation in the comments.
The implementation details of this might be tricky. The dom lib generator could theoretically produce two
.d.ts
outputs for each of today's files: one withany
and one withunknown
. Or, if two files aren't doable, there could be an intrinsic declared that switches betweenany
andunknown
.The text was updated successfully, but these errors were encountered: