microsoft / microsoft/TypeScript

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

オープン
#57,711 コメント 5 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
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. リポジトリをフォークし、ブランチを切って変更します。
  4. 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 を短くまとめたダイジェスト。