Inferred type parameter does not satisfy constraint

Offen
#45,286 0 Kommentare 6 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

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

Rechercherichtung

Reproduziere das verlinkte TypeScript Playground-Beispiel und vergleiche das gemeldete Verhalten mit TypeScript 3.3 und 3.5+. Beginne damit, die Inferenz generischer Typen und die Constraint-Prüfung für die gezeigten Funktionsaufrufe nachzuverfolgen. Als abgeschlossen gilt die Aufgabe, wenn der Compiler die inferierte U extends T-Constraint konsistent behandelt und eine Regressionstestabdeckung für das ausgewählte erwartete Verhalten vorhanden ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

Bug Domain: check: Type Inference

Bug Report

🔎 Search Terms

generic constraint, inference, invalid, specified, manually, explicitly, invalid

🕗 Version & Regression Information
  • This changed between versions 3.3 and 3.5
⏯ Playground Link

Playground link with relevant code

💻 Code
declare function f<T, U extends T>(cb: (u: U) => T): U

const v = f(u => "x");
// const v: unknown <-- wait, what?
// function f<string, unknown>(cb: (u: unknown) => string): unknown

f<string, unknown>(u => "x"); // error, of course
// -----> ~~~~~~~
// Type 'unknown' does not satisfy the constraint 'string'.
🙁 Actual behavior

In TS3.5+, the type parameter T is inferred to be string, and U is inferred to be unknown in the first call to f(). But then U extends T is not met; you couldn't even manually specify T and U like this when calling f(), as shown in the second call to f().

(In TS3.3, T and U are both inferred as {}.)

🙂 Expected behavior

Either I'd expect both T and U to be string, or I'd expect an error where the compiler says it cannot find a suitable candidate for U. Presumably the compiler has not inferred T before it needs to infer U, and so it constrains U to T's constraint which is implicitly unknown, and then falls back to that. But it never circles back to make sure that such a fallback actually works for U extends T.


I came upon this while looking for an answer to this SO question. This behavior is weird, right? I haven't found an existing issue for it.

Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus microsoft/TypeScript

Alle Issues in microsoft/TypeScript

Ähnliche Issues

Weitere Issues zu Go

Neue Issues direkt in Ihr Postfach

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