microsoft / microsoft/TypeScript
[Regression] TS2562 - mixins cannot accept generic types
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
TypeScript Version: 2.6.0 and more
Search Terms: mixin, 2.6.
Code
export interface Constructor<T = any> extends Function {
new(...args: any[]): T;
}
class A<T> {
public a: T;
}
class B<T> {
public b: T;
}
function Mixin<T>(...classes: any[]): Constructor<T> {
return function() {
// whatever, not the purpose of the demonstration
} as any;
}
interface IAB<T, U> extends A<T>, B<U> {}
// on typescript 2.6.0 and more, its no more possible to use generic types into mixins....
class AB<T, U> extends Mixin<IAB<T, U>>(A, B) {
}
// on typescript 2.6.0 we're forced to write
// class AB<T, U> extends Mixin<IAB<any, any>>(A, B) {}
// so we loose type checking...
// intentional type error
class testClass extends AB<boolean, string> {
public a: number; // before 2.6.0 => type of 'a' properly detected as wrong, boolean expected
}
Expected behavior:
Like before typescript 2.6.0, extending a class with a mixin which takes generic types should be allowed.
Using mixin is a really common usage in js/ts to build classes which implement/inherit properties from more than one classe (sometimes named 'factories'). Before 2.6.0, typescript properly detected the union of class having generic types, but currently this generates TS2562 errors, so its no more possible to construct typed generic mixins.
In a more generic way:
function A<T>() {
return class {};
}
// should be allowed
class B<T> extends A<T>() {}
Actual behavior:
TS2562, Base class expression cannot reference class type parameters (on T and U)
Playground Link: here
Related Issues: Partially related https://github.com/Microsoft/TypeScript/issues/19668
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
Inizia riproducendo TS2562 con l’esempio di codice fornito o Playground Link usando le versioni di TypeScript interessate. Analizza il comportamento del checker per i parametri di tipo generici della classe a cui fanno riferimento le espressioni della classe base dei mixin, quindi aggiungi una copertura che dimostri che il mixin generico viene accettato e che l’errore intenzionale del tipo della proprietà continua a essere rilevato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 35/100