microsoft / microsoft/TypeScript

Giving an error type to types that have failed to resolve, instead of any

Offen
#61,089 4 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

🔍 Search Terms

Types that fail to import should not be typed as any. SkipLibCheck. Type resolution failure.

✅ Viability Checklist
⭐ Suggestion

Currently if TS fails to resolve a type, for example because it can't resolve an import, the type is set as any. TS will display the type by it's alias (the local name given to the type), hiding that fact that it is any underneath. any being a special type that largely disables type checking, being silently introduced, can hide code errors.

Failing imports are typically reported by TS as an error, even if using that any typed type later on doesn't produce an error. However if there is a type resolution failure within an external module with skipLibCheck turned on (which is widely used and recommended, for multiple reasons), then there is no error and an any type is silently introduced to the project types.

The suggested solution is instead type these types with an error type, working name: failed. An assignment to or from a failed type is an error, like an opposite to any.

--

I've ticked non-breaking, even though it could lead to compilation of existing code failing, because it would only be highlighting an error that previously wasn't highlighted. It's non-breaking in valid code.

There would be a degree of annoyance never-the-less in triggering compilation errors in code that previously compiled, caused by problems in external libraries. Error messages indicating where the type resolution failed would be very helpful here.

I'm putting this out for discussion, because it's difficult for me to foresee the full consequences and challenges of such a change.

📃 Motivating Example

In this simple Playground we import a type from a package with an internal resolution error, that leads a type X['c'] to be any. TS reports no errors to us and lets us perform type-irrational operations.

💻 Use Cases

Fairly obviously, it's a safeguard against mistakes in external libraries, and it would also make broken imports more obvious by showing errors at the use site not just at the import site.

This occurred to me when dealing with a typing issue in @stripe/stripe-js (1.3M weekly downloads) stripe-js#714. Just fixing the import in the index file as the maintainer suggested would have introduced this issue to that library.

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

Beginne mit dem verknüpften Playground-Beispiel und der Diskussion des Vorschlags über fehlgeschlagene Importe, externe Module und skipLibCheck. Untersuche, wie nicht aufgelöste Typen derzeit zu any werden, und ermittle das beabsichtigte Verhalten und die Diagnosen für einen vorgeschlagenen fehlgeschlagenen Typ. Als abgeschlossen gilt die Ausarbeitung eines abgestimmten Designs, das das zugrunde liegende Beispiel behandelt, ohne die Typprüfung stillschweigend zu schwächen.

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
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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