microsoft / microsoft/TypeScript

Recursive definitions in mixins

Aperta
#29,872 8 commenti 17 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Its currently possible to create a circular references in classes just fine:

export class Class1 {
    another     : Class2     // compiles fine
}
export class Class2 {
    another     : Class1    // compiles fine
}

However, the same thing fails when using mixin-based classes:

export const SampleMixin1 = <T extends AnyConstructor<object>>(base : T) =>
class SampleMixin1 extends base {
    another             : SampleMixin2 // TS2502: 'another' is referenced directly or indirectly in its own type annotation
}
export type SampleMixin1 = Mixin<typeof SampleMixin1>


export const SampleMixin2 = <T extends AnyConstructor<object>>(base : T) =>
class SampleMixin2 extends base {
    another             : SampleMixin1 // TS2502: 'another' is referenced directly or indirectly in its own type annotation
}
export type SampleMixin2 = Mixin<typeof SampleMixin2>


// supporting declarations for mixin pattern
export type AnyFunction<A = any>      = (...input: any[]) => A
export type AnyConstructor<A = any>   = new (...input: any[]) => A
export type Mixin<T extends AnyFunction> = InstanceType<ReturnType<T>>

This makes things much more complicated, you need to introduce some dummy interfaces, etc, etc.

The workaround exists - to use different form of creating the type for the standalone mixin class - with interfaces:

export const SampleMixin3 = <T extends AnyConstructor<object>>(base : T) =>
class SampleMixin3 extends base {
    another             : SampleMixin4
}
export interface SampleMixin3 extends Mixin<typeof SampleMixin3> {}


export const SampleMixin4 = <T extends AnyConstructor<object>>(base : T) =>
class SampleMixin4 extends base {
    another             : SampleMixin3
}
export interface SampleMixin4 extends Mixin<typeof SampleMixin4> {}

But this notation seems to drive crazy the IDE (the language server under the hood?). It stop finding usages of properties, show many false type errors etc. Basically in such approach you are limited to old-good console npx tsc launch (which works well at least), but all the development aid facilities don't work.

I think mixin pattern should be supported first class. I'd even create some language construct for it.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con gli esempi di classi ricorsive e mixin nell’issue, confrontando il comportamento dei riferimenti diretti alle classi con le soluzioni alternative basate su alias di tipo e interfacce. Il lavoro è completo quando le definizioni ricorsive nei mixin sono supportate senza interfacce fittizie, mentre la navigazione degli utilizzi delle proprietà e il controllo dei tipi continuano a funzionare correttamente.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.