microsoft / microsoft/TypeScript

Mixins created from `obj.constructor` cannot have members

Aperta
#61,255 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: classes Help Wanted Possible Improvement
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔎 Search Terms

mixin constructor property instance

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about mixins
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.3#code/C4TwDgpgBAwg9gOwM7AE4FcDGw6oDwAqAfFALxQIQDuAFAHQMCGqA5kgFxSMIgDaAugEoyJAgG4AUBMwAbRkiRQAYnDhQA3hKhQA9DqgB1XAGsuqOOgQATKAAtgwMBz0sAlsFvoARnUxwAtjr+rpjmSHAAZsA6BOAQAMqhrmDRrgroEEg6AMwAHAAsAIxaUFYQsszQvABEfshoWDio1fyc8PUY2Lh4HmlEkgC+UhGW2K6IUACyrgAergiFhFAQM8AQ1ortKJ1NeCpwREQ0SOiQqBUKnATCmtp6UADyANIlqBDA6KgIUBeKK2sbKAnM6-DQlbQRVQRGg3IbaAaDYajYDjb7TOYIABMS3+6ysin2RzgXgAVlcbiV7jA5AooAByGgAQQQiBA-gsil+gjpUHmflQb2wMhAy1WeMUXnk0FBdIIdLolP09KZLJ47PQnJpSG5vMU8iQrhYCEYXhk0BwUA80sQ20Y82AUEilri9LlABooF50A7ZTy-OgZDYvNB5ihuCjGGsbFR3LYuKVXBEIhA3ggHScvKBII6Ij8bWg7Wn6ft5a93p9vqDcYDiSTfPmdrgwdoIVCYRo4VAERIhkA

💻 Code
type Constructor<T> = new(...args: any[]) => T;

class Foo {
  // Work around https://github.com/microsoft/TypeScript/issues/3841
  declare ["constructor"]: Constructor<this>;
}

function Mixin1<T extends Constructor<Foo>>(superclass: T) {
  // OK
  return class extends superclass {
    foof() {}
  };
}

function Mixin2<T extends Foo>(obj: T) {
  // Class '(Anonymous class)' incorrectly extends base class 'T'.
  //  '(Anonymous class)' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'Foo'.
  return class extends obj.constructor {
    foof() {}
  };
}
🙁 Actual behavior

Mixin1 compiles fine, but Mixin2 has an error:

Class '(Anonymous class)' incorrectly extends base class 'T'.
  '(Anonymous class)' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'Foo'.

Removing the foof member causes it to compile without error.

🙂 Expected behavior

Both should compile fine because they are doing the same thing.

Additional information about the issue

No response

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 eseguendo la riproduzione collegata in TypeScript Playground e confronta Mixin1 con Mixin2, concentrandoti sull’espressione di ereditarietà della classe che usa obj.constructor. L’issue è completata quando Mixin2 può mantenere il suo membro foof senza l’errore di ereditarietà segnalato, mentre Mixin1 continua a compilare.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.