microsoft / microsoft/TypeScript

Unable to specify return type of class extends T implements IFooable

Offen
#15,001 6 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

In Discussion Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

TypeScript Version: 2.2.1 / nightly (2.2.0-dev.201xxxxx)

Code

// A *self-contained* demonstration of the problem follows...
export type Constructor<T> = new (...args: any[]) => T;
export interface IFooable { FooProp: string; }
export function FooableMixin<T extends Constructor<{}>>(Base: T) {
    return class extends Base implements IFooable {
        FooProp: string;
        constructor(...args: any[]) {
            super(...args);
        }
    }
}
export class BaseBar { BarProp: string = "baz"; }
export class FooableBar extends FooableMixin ( BaseBar ) {}
let foobar = new FooableBar();
foobar.FooProp = foobar.BarProp; 
/* This is ok since FooableBar extends an Anonymous class inheriting from BaseBar and implementing IFooable */

If I turn on "declaration: true" in tsconfig.json this constuct breaks since the Anonymous class counts as private and not exported type (error TS4060) which is kind of natural since it is Anonymous.

What we need is a way to describe Classes as return types.

Expected behavior:

A d.ts file with a type declaration for the Mixin function Fooable that defines a Anonymous Class return type in the style of:

declare function Fooable(T extends Constructor<{}>): (class extends T implements IFooable);

Actual behavior:

TS4060 error when compiling with the "declaration" flag.
If casting the Anonymous Class return value to or T, the interface is lost.

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 dem eigenständigen FooableMixin-Beispiel im Issue und kompiliere es mit aktivierter Declaration-Ausgabe. Untersuche die Declaration-Ausgabe für die von FooableMixin zurückgegebene anonyme Klasse; als erledigt gilt die Aufgabe, wenn die generierte .d.ts die Klasse beschreibt, die T erweitert und IFooable implementiert, ohne TS4060 oder den Verlust des Interfaces.

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

Neue Issues direkt in Ihr Postfach

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