Skip to content
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

Compiler option to switch lib .d.ts anys to unknown #60899

Open
6 tasks done
JoshuaKGoldberg opened this issue Jan 2, 2025 · 2 comments
Open
6 tasks done

Compiler option to switch lib .d.ts anys to unknown #60899

JoshuaKGoldberg opened this issue Jan 2, 2025 · 2 comments
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@JoshuaKGoldberg
Copy link
Contributor

JoshuaKGoldberg commented Jan 2, 2025

🔍 Search Terms

compiler option flag lib.d.ts noImplicitAny unknown any

✅ Viability Checklist

⭐ Suggestion

A few built-in (lib*.d.ts) types include any, even though unknown would be safer. Most notably:

These are kept as any for legacy support reasons: it would be a massive breaking change to restrict them to unknown. But, for newer projects and those that don't rely on those anys, switching them to unknown would be much safer long-term.

Proposal: could we have a compiler option to switch the lib definition anys to unknowns? Maybe, strictLibDefinitions? useUnknownInLibDefinitions? (I'm not convinced of those specific names)

📃 Motivating Example

Using this compiler option will prevent many of the common anys from sneaking into projects that use built-ins such as JSON.parse and Response.

For example, this code does not have a type error by default, but would with the new compiler option enabled:

const data = JSON.parse(`"clearly-a-string"`);
//    ^? any (today)
//    ^? unknown (with this compiler option)

console.log(data.some.property.that.does.not.exist);

💻 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 use JSON.parse et al.

This is not the same as #27265. That issue tracks a flag to handle implicit anys 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 with any and one with unknown. Or, if two files aren't doable, there could be an intrinsic declared that switches between any and unknown.

@MartinJohns
Copy link
Contributor

Duplicate of #26188.

@JoshuaKGoldberg
Copy link
Contributor Author

Ah, I meant to mention that in the OP, thanks. I'd interpreted that one as suggesting making the change always - with varying levels of compiler-option-orientation in the comments (e.g. #26188 (comment) against it).

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants