microsoft / microsoft/TypeScript

JavaScript class property becomes `any` without warning when used in a function that is assigned to itself

Offen
#57,711 5 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.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔎 Search Terms

jsdoc class null any

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

https://www.typescriptlang.org/play?strictNullChecks=true&useDefineForClassFields=true&filetype=js#code/KYDwDg9gTgLgBAYwDYEMDOa4DEITgbwCg4S4B6AKguNLgrgAEwUoUBbAgOwFc2AjYFAD8AXzhsUATwEA5XjVIUyCxBE5oYUbghjQAFBOnA5bAJQEVtGAAsAlmgB0h2bzgBeODyRIA3IUukNvYOaMAwALJSLmx6pn60Iv60oRFRxryxFrRWdo7O6RwekTZOKCB6QXlpJnEqiSqU1Nn0TCzsBBpQtpwA5mKdlkoqMCgA1sBoAMqa3T16neb49bQAZriZRNmBuQ4j41MzvRU7+TXxW+RkF9cAenf3D-cAtC9PnniCUNAANOLVrvY4ChOJI6oREkA

💻 Code
export class Foo {
    /**
     * @param {number?} maybeNum
     */
    constructor(maybeNum) {
        this.maybeNum = null;

        this.setMaybeNum();
    }

    setMaybeNum() {
        this.maybeNum = Math.max(this.maybeNum);
    }

    /**
     * @param {string} str
     */
    takesString(str) {}

    foo() {
        this.takesString(this.maybeNum);
        //               ^^^^^^^^^^^^^---- no error, maybeNum is any
    }
}
🙁 Actual behavior

this.maybeNum is any

🙂 Expected behavior

this.maybeNum is number | null or (since noImplicitAny is set) an error is emitted warning the user that this.maybeNum is any.

Additional information about the issue

It seems like the core issue here is this line:

this.maybeNum = Math.max(this.maybeNum);

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

Beginnen Sie mit dem verknüpften TypeScript Playground-Beispiel und der Zuweisung this.maybeNum = Math.max(this.maybeNum) in der JavaScript-Klasse. Verfolgen Sie, wie sich diese Zuweisung auf den späteren Aufruf takesString(this.maybeNum) auswirkt, und überprüfen Sie anschließend die Korrektur anhand des erwarteten Typs number | null oder einer noImplicitAny-Diagnose.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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