microsoft / microsoft/TypeScript
Add a new type of class declaration to support mixins
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔍 Search Terms
"mixin", "InstanceType", "typeof", "class"
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ 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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Non sono indicati file sorgente, test o punti di ingresso del compilatore. Inizia esaminando la sintassi as type proposta e le soluzioni alternative esistenti InstanceType<typeof Result> e quelle per le sottoclassi. Il lavoro sarebbe completo quando fosse definito un design per dichiarazioni che associano un valore a un tipo senza modificare il JavaScript emesso.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100