microsoft / microsoft/TypeScript

Interface merging for any property names does not allow type narrowing

Offen
#40,904 2 Kommentare 4 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

TypeScript Version: 4.1.0-dev.20201002

Search Terms: interface merging merge object any name subsequent property declarations

Code

interface PropsContainer {
  props: {
    [propName: string]: any;
  }
}

interface PropsContainer {
  props: {
    [propName: string]: any;
    specialProp: string;
  }
}

Expected behavior:
This should be allowed. The merged interface should result in a type with props that have any prop name as any except for specialProp which is a string.

This would be useful in a case where a third party library (such as @types/react-test-renderer) declare an object type with { [propName: string]: any } syntax, but you want to narrow the value of the property for specific types via interface merging.

Note that even { specialProp?: any, [propName: string]: any } is not allowed even though the types are functionally identical in this case.

Actual behavior:
This results in a Subsequent property declarations must have the same type.

Playground Link: https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgApQPYAcDOBhDcOUaZAbwChlktNcAucq65AbVuwDk4BbCRnGCigA5gF1GcEAE8A3MwC+FJRVCRYiFOmz5CYYiFKVqHBkxbIcWCAmBwANtqwB+STPkX2dbnwFDREshScorKQA

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 der verlinkten TypeScript Playground-Reproduktion und bestätige die Diagnose für die beiden zusammengeführten PropsContainer-Deklarationen. Verfolge anschließend den Type-Checking-Pfad für nachfolgende Eigenschaftsdeklarationen und Indexsignaturen. Fertig ist es, wenn der gezeigte Merge akzeptiert wird, specialProp auf string eingeengt wird und bestehende inkompatible Neudeklarationen weiterhin abgelehnt werden.

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
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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