microsoft / microsoft/TypeScript

Compiler option to switch lib .d.ts `any`s to `unknown`

Offen
#60,899 3 Kommentare 17 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Needs Proposal Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

🔍 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 https://github.com/microsoft/TypeScript/issues/26188. Per https://github.com/microsoft/TypeScript/issues/60899#issuecomment-2567457149: 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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit den zitierten Definitionen in src/lib/es5.d.ts und src/lib/dom.generated.d.ts und untersuchen Sie anschließend, wie der lib-Generator und Compileroptionen Deklarationsdateien auswählen. Vergleichen Sie das vorgeschlagene any-zu-unknown-Verhalten für JSON.parse, Response.json() und Storage und definieren Sie Tests, die zeigen, dass die Option die Typprüfung ändert, ohne das ausgegebene JavaScript zu beeinflussen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
28/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.