microsoft / microsoft/TypeScript

Inference isn't working with constructor that contextually types 'this' with intersections of itself

Offen
#12,846 2 Kommentare 2 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
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

TL;DR: There should be no errors in the below example.

I am trying to model a library that takes an options bag for its constructor, and exposes those properties onto the instance that is eventually produced.

For example, if I wrote

let instance = new Component({
    methods: {
        hello() { }
        world() {
            this.hello();
        }
    }
});

I should be able to write instance.hello() or instance.world().

Additionally, since this in world will be bound to the instance, this.hello should be legal and well-typed in the above example.

However, I'm not able to come up with a version of this that works.

/**
 * Ensures all properties have a `this` param of type `T`.
 */
interface ThisEnforced<T> {
    [prop: string]: (this: T, ...args: any[]) => any;
}

interface ComponentOptions<Methods> {
    methods?: Methods
}

interface Component<Methods extends ThisEnforced<Component<Methods> & Methods>> {
    $methods: Methods;
}

interface ComponentStatic {
    new <Methods extends ThisEnforced<Component<Methods> & Methods>>(options: ComponentOptions<Methods>):
        // Returns...
        Component<Methods> & Methods;
}
declare var Component: ComponentStatic;

let inst = new Component({
    methods: {
        hello() {
            // ...
        },
        world() {
            this.hello;
            //   ~~~~~
            // Error: Property `hello` does not exist on this type.
        }
    },
});

inst.hello;
//   ~~~~~
// Error: Property `hello` does not exist on this type.

In the above example, there should be no errors - however, it appears that no inferences are being drawn as a type argument for Methods, and both this.hello and inst.hello are causing errors.

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 damit, das Beispiel zur kontextbezogenen Typisierung und Konstruktor-Inferenz im Issue zu reproduzieren, wobei du dich auf den this-Typ innerhalb von world und den inferierten Typ von inst konzentrierst. Verfolge die Pfade der TypeScript-Typprüfung und der generischen Inferenz, die daran beteiligt sind. Als erledigt gilt es, wenn das Beispiel keine Fehler erzeugt, this.hello akzeptiert wird und inst.hello mit einer geeigneten Typisierung verfügbar ist.

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
Klar beschrieben
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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