microsoft / microsoft/TypeScript

Mixin constructor not working with `args:unknown[]`, only with `args:any[]`

Offen
#60,694 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔎 Search Terms

site:github.com/microsoft/typescript mixin class "unknown" args any args

🕗 Version & Regression Information
  • This changed most likely after unknown was introduces and the bespoke/unique mixin type checking logic wasn't updated.
⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAwg9gOwM7AE4FcDGw6qgXigQgHcoAKAOmoENUBzJALinQQGsE4SEBtAXQCUBAHxQ4AIwBWEbACg5AMzbYAloigAxOHAA8AFSgQAHsAgIAJkliIUGbLhHkAQjSQQW+4QG85Uf1CoEMDoqAhQmAA2btbacEam5lZQru5QvgGZEbZoWDioVLQMzKwcXDwCPn5ZNUjokAXUlHSMgtU1AL4KNf6KOgRQAIwATADM3T19cACywQAWcBbkPoHBoeHAc6pIlFNQXZldXUA

💻 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:

playground

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der verknüpften TypeScript Playground-Reproduktion und verfolge die im Bericht beschriebene spezielle oder einzigartige Mixin-Typprüfungslogik. Vergleiche die bereitgestellten Konstruktorbeispiele mit unknown[] und any[], und überprüfe anschließend, dass die Regressionstests das erwartete Mixin-Verhalten abdecken.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.