microsoft / microsoft/TypeScript
Needless evaluation of placeholder type when checking that an interface matches a constraint
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
Bug Report
🔎 Search Terms
type constraint, interface, evaluation, type checker, placeholder type
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about this
⏯ Playground Link
Playground link with relevant code
💻 Code
Note: the utility type Zeros is designed to infinitely recur when it is fed unknown in order to exhibit the problem.
type Zeros<N, R extends unknown[] = []> =
N extends R['length'] ? R : Zeros<N, [0, ...R]>;
interface $Zeros { 0: unknown; type: Zeros<this[0]>; }
type apply$Zeros = apply<$Zeros, [3]>;
// ~~~~~~~~~~~~~~~~~~
// Type instantiation is excessively deep and possibly infinite
type apply<$T extends { type: unknown }, Args> = ($T & Args)['type'];
Alternate implementations of apply which don't reproduce the error can be found bellow:
// no type constraint
type apply<$T, Args> = ($T & Args)['type' & keyof $T];
// type constraint cheking keyof
type apply<$T extends (keyof $T extends keyof Type ? unknown : never), Args> =
($T & Args)['type' & keyof $T];
🙁 Actual behavior
It is my understanding that the type constraint $T extends { type: unknown } causes the type checker to evaluate Zeros<this[0]> as Zeros<unknown>, which leads to infinite recursion.
I believe this evaluation was not necessary because every type extends unknown, including undefined or never (an optional type field could also be caught without having to evaluate the value), therefore I consider that $T extends { type: unknown } could only check that the type key exists in $T without relaxing type safety.
I imagine that skipping this evaluation would also improve performance.
The alternative type safe solution of manually checking the existence of the keys is not user friendly.
🙂 Expected behavior
$T extends { type: unknown } should only check that the type field exists and enable to index $T & Args with type with no unexpected side-effect.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, das verlinkte Playground-Beispiel zu reproduzieren, und vergleiche es mit den beiden alternativen apply-Implementierungen im Issue. Der Payload nennt weder eine TypeScript-Quelldatei noch einen Test, daher erfordert die Nachverfolgung der Auswertung der Type-Checker-Constraint Vertrautheit mit dem Repository. Die Aufgabe ist erfüllt, wenn die Constraint den vorgesehenen Typ akzeptiert, ohne den rekursiven Platzhalter auszuwerten, und das bestehende Type-Safety-Verhalten intakt bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 28/100