microsoft / microsoft/TypeScript

Polymorphic `this` breaks when a module exports a variable and interface with the same name

Offen
#19,397 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Importing a variable that has a corresponding interface of the same name from a module causes unexpected behavior.

In the screenshot below, the function, doSomething, should return the type Model<number> due to polymorphic this; however, it returns Model<T>, which is unusable.

screen shot 2017-10-20 at 11 02 03 pm

Here is an easy way to reproduce this behavior:

  1. npm install -g @types/sequelize sequelize
  2. Replace node_modules/@types/sequelize/index.d.ts with the following
declare namespace sequelize {
    interface Model<T>  {
        doSomething(arg: T): this;
    }
}

declare var sequelize: { Model: sequelize.Model<any> };

export = sequelize;
  1. create a file with the following contents:
import { Model } from 'sequelize';

let a: Model<number> = null as any;


a.doSomething(5).doSomething();

Workaround:
Use import * as ... syntax and the names will resolve correctly.

screen shot 2017-10-20 at 11 22 28 pm

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 Problem mit der bereitgestellten sequelize-Deklaration und dem Importieren von Model mit Named-Import-Syntax zu reproduzieren. Verfolge, wie der Compiler die exportierte Variable und das gleichnamige Interface bei der Prüfung von polymorphem this auflöst. Als abgeschlossen gilt, wenn das Beispiel Model ableitet und den verketteten Aufruf erlaubt, ohne den Namespace-Import-Workaround zu erfordern.

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
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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