microsoft / microsoft/TypeScript
Polymorphic `this` breaks when a module exports a variable and interface with the same name
Dieses Issue hat noch niemand übernommen.
- 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.
Here is an easy way to reproduce this behavior:
npm install -g @types/sequelize sequelize- Replace
node_modules/@types/sequelize/index.d.tswith the following
declare namespace sequelize {
interface Model<T> {
doSomething(arg: T): this;
}
}
declare var sequelize: { Model: sequelize.Model<any> };
export = sequelize;
- 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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Ö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