microsoft / microsoft/TypeScript
Mixin constructor not working with `args:unknown[]`, only with `args:any[]`
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 Search Terms
site:github.com/microsoft/typescript mixin class "unknown" args any args
🕗 Version & Regression Information
- This changed most likely after
unknownwas introduces and the bespoke/unique mixin type checking logic wasn't updated.
⏯ Playground Link
💻 Code
type Constructor = new (...args: unknown[]) => object
function Foo<T extends Constructor>(Base: T) {
return class Foo extends Base { // <---------------- ERROR
constructor(...args: unknown[]) {
super(...args)
}
foo = 123
fooMethod() { return this.foo }
}
}
Change the unknown to any and the error goes away. You'd think they should behave the same: you don't care what the args are, you just need to pass them along.
Here's how to fix the error, changing unknowns to anys:
Code:
type Constructor = new (...args: any[]) => object
function Foo<T extends Constructor>(Base: T) {
return class Foo extends Base { // <---------------- ERROR
constructor(...args: any[]) {
super(...args)
}
foo = 123
fooMethod() { return this.foo }
}
}
🙁 Actual behavior
Type error on Foo.
🙂 Expected behavior
I expected it to allow "any unknown args" to be passed along.
Additional information about the issue
Plus, with an unknown type, the code in the mixin constructor can actually be more type safe, requiring type narrowing or type casting to use the args at all, whereas any allows anything to be done to the args which is not safe.
Maybe the mixin type checking algo could cast the base class constructor to new (...args: any[]) and simply allow anything to be passed in, to make unknown work. Then the mixin type checking algo could still enforce any[] as well as (more preferably) unknown[].
Also, maybe it would make sense to have an implicit Constructor type built into the mixin type checking algo so that mixins can just be really easy for plain JS users who are migrating to TypeScript.
I've met multiple people who are baffled by how difficult it is to write mixins in TypeScript when they come from plain JavaScript.
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 dalla riproduzione collegata in TypeScript Playground e segui la logica specifica o unica di controllo dei tipi dei mixin descritta nel report. Confronta gli esempi di costruttori forniti che usano unknown[] e any[], quindi verifica che la copertura delle regressioni dimostri il comportamento previsto dei mixin.
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
- 42/100