microsoft / microsoft/TypeScript

Type argument with a subset of the parameter constraint's optional keys incorrectly reported as unassignable to constraint

Offen
#63,725 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: Mapped Types
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

🔎 Search Terms

"does not satisfy the constraint" "keyof" "optional"

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "constraint"
⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAsiAq4IB54BooFUoQB7AgDsATAZygG8oBtAaSgEtCoBrCEAewDMp4BdAPwAuKAFdCLQhwDuzAL4A+KAF4sAbgCwAKG0BjADYBDUuTgBhIydRKK2qFABuh-aIgBCEXESRUGKnUZmNk4eeCgAMihSYAAnJgBzQRFxSRl5BU0tOW0gA

💻 Code
type MyType<T, U extends { [K in keyof T]?: unknown }> = U;

class MyClass<T> {
  value!: MyType<T, { [K in keyof T & string]?: unknown }>;
}
🙁 Actual behavior

The following error is reported:

Type '{ [K in keyof T & string]?: unknown; }' does not satisfy the constraint '{ [K in keyof T]?: unknown; }'.
  Type 'keyof T' is not assignable to type 'keyof T & string'.
    Type 'string | number | symbol' is not assignable to type 'keyof T & string'.
      Type 'string' is not assignable to type 'keyof T & string'.
        Type 'string' is not assignable to type 'keyof T'.
          Type 'keyof T' is not assignable to type 'string'.
            Type 'string | number | symbol' is not assignable to type 'string'.
              Type 'number' is not assignable to type 'string'.
🙂 Expected behavior

No error should be reported because due to the keys being optional, { [K in keyof T & string]?: unknown } is a subtype of { [K in keyof T]?: unknown } for all T.

Additional information about the issue

No response

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

Reproduziere die Diagnose aus dem verlinkten TypeScript Playground-Beispiel und verfolge anschließend die Prüfung generischer Constraints für gemappte Typen mit optionalen Eigenschaften und intersected keyof keys. Die Arbeit ist abgeschlossen, wenn das Beispiel ohne Fehler kompiliert und gleichzeitig die korrekte Constraint-Prüfung für verwandte Fälle erhalten bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

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