microsoft / microsoft/TypeScript

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

Open
#57,711 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 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);

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground example and the assignment this.maybeNum = Math.max(this.maybeNum) in the JavaScript class. Trace how that assignment affects the later takesString(this.maybeNum) call, then verify the fix against the expected number | null type or a noImplicitAny diagnostic.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.