microsoft / microsoft/TypeScript

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

未关闭
#57,711 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Awaiting More Feedback Suggestion
主要语言
Go
星标
111k
派生
14.4k
平均合并
1 天 19 小时
30 天内合并 PR
117

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从链接的 TypeScript Playground 示例以及 JavaScript 类中的赋值 this.maybeNum = Math.max(this.maybeNum) 开始。跟踪该赋值如何影响后续的调用 takesString(this.maybeNum),然后根据预期的 number | null 类型或 noImplicitAny 诊断验证修复。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, typescript
领域
compilers
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。