microsoft / microsoft/TypeScript

Add a new type of class declaration to support mixins

Offen
#60,254 7 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

🔍 Search Terms

"mixin", "InstanceType", "typeof", "class"

✅ Viability Checklist
⭐ Suggestion

When I run a mixin function, it outputs just the value of the returned expression, not the type

function myMixin<T extends new (...args: any[]) => any>(ctor: T) {
    return class extends ctor {
        a = 1;
    }
}

class MyClass {
    b = 2
}

const Result = myMixin(MyClass);

//             ↓ Result refers to a value, but is being used as a type here. Did you mean typeof Result ?          ↓ 
const istance: Result = new Result();

So I would need to define the type too

// ...
const Result = myMixin(MyClass);
type Result = InstanceType<typeof Result>;
// ...

I suggest adding a new syntax to define both a variable and a type with the same name

// ...
const Result = myMixin(MyClass) as type; // "as class" is also a viable option
// ...

Examples:

namespace Something {
    export class Idk {
        c = 1;
    }
}

const a = Something.Idk as type; // Ok

function someFunc() {
    return class {
        e = 1;
    }
}

const b = someFunc() as type; // Ok
const c = 1 as type; // Error: Expression "1" doesn't have any type attached
⭐ OPTIONAL extra generalised suggestion

Additionally, it would be nice if we could be able to generalise this process for all variables that also have an attached type

const a = 1;
type a = 2; // The type is unrelated to the value, no usage of this comes to my mind at the moment, but it already works

const b = a;
type b = a;
// Or
const b = a as type; // Defines a value from the `a` variable and a type from the `a` type

But it wouldn't work out of the box for the main suggestion, since Result doesn't actually have any type attached it would require functions (myMixin() for example) to be able to have attached return types

📃 Motivating Example

(Mentioned on the suggestion body)

💻 Use Cases
What do you want to use this for?

To improve the development of mixins in general

What shortcomings exist with current approaches?

Too verbose and feels like something it should to by default

const Result = myMixin(MyClass);
type Result = InstanceType<typeof Result>;

Creates an actual runtime class, that surely adds an overhead, althought it's minimal

class Result extends myMixin(MyClass) { }
What workarounds are you using in the meantime?

The runtime class one

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

Es werden keine Quelldateien, Tests oder Compiler-Einstiegspunkte genannt. Beginnen Sie mit der Prüfung der vorgeschlagenen Syntax as type sowie der bestehenden Workarounds InstanceType<typeof Result> und für Subklassen. Die Arbeit wäre abgeschlossen, wenn ein festgelegtes Design für Deklarationen vorliegt, die einen Wert mit einem Typ verknüpfen, ohne das erzeugte JavaScript zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
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.