[7.1 Regression] Unexpected behaviour change from Stronger Distributive Type checks

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

メンテナーはふだん 1 日以内に返信

@ahejlsberg がすでに取り組んでいます。

2026年10月21日 から。

  • #64307 @Andarist による — オープン

評価

この issue はまだ評価されていません。

説明

Needs Investigation
🔎 Search Terms

7.1 regression, TS2345

🕗 Version & Regression Information
  • This changed between versions 7.0.2 and 7.1.0-beta
⏯ Playground Link

https://www.typescriptlang.org/play/?#code/KYDwDg9gTgLgBAYwDYEMDOa4GE4G8BQcRcAtgIwA8AKgHwAUhxTAljMCQFxxUA0jTRBCiRIARigQBrLlTig2AOwAmmNDCjMFAc34CmAfjh1W7GQEo4AXhpwAbhGZLdeol2NtO3C9bsOnTMy57RzxnYiERcSl3djMAbjDBYTEJSRiSOHRuORBFFTg1DW04Q1lzBKYAX3xdEgAmBhc4E09CzS0+JoiUqX03Fq427W8bYP9iQN9HCuJ66npE5o8ZTpduqOkjAa8rUb9dSbGZojnaRpdt3kX11JkcvNV1dsWDI3TzXamlCwAfOABXZTAABmmmA4yari2yx2PjGByCflCTWYwKMNykFgIkPCyQ26XiL1xkVS6UymFk8mAykeRS0JWy5UW1Sq+GqQA

💻 Code
export class C {
    m1<T>(
        item: T,
        callback: T extends string
            ? (item: T) => void
            : (item: T) => void
    ): void {
        callback(item);
        callback(item as T extends string ? T : T);
    }

    m2(
        item: string,
        callback?: (item: string) => void
    ): void;
    m2<T>(
        item: T,
        callback: (item: T) => void
    ): void;
    m2<T>(
        item: T,
        callback: T extends string
            ? ((item: T) => void) | undefined
            : (item: T) => void
    ): void {
        if (callback) {
            callback(item);
            callback(item as T extends string ? T : T);
        }
    }
}
🙁 Actual behavior
src/main.ts:8:18 - error TS2345: Argument of type 'T' is not assignable to parameter of type 'T'.
  'T' is only assignable to the non-distributed 'T', but 'T' has been distributed here.

8         callback(item);
                   ~~~~

src/main.ts:27:22 - error TS2345: Argument of type 'T' is not assignable to parameter of type 'T'.
  'T' is only assignable to the non-distributed 'T', but 'T' has been distributed here.

27             callback(item);
🙂 Expected behavior

No Error.

Additional information about the issue

In case of m1, if tsc is aiming to strengthen Distributive Type checks, I would hope tsc can be smart enough to "undistribute" type whenever it hits some identical declarations on different branches. As for how to define identical, I suppose it may deserve further thinkings and discussions.

For m2, we have a common way of writing overload in typescript. The condition T extends string is there for determining if a method argument is optional or not. The (strengthened) disributive check that follows is kinda unintended & unwanted effect in this case. It would be weird to let the ? T : T trick to become a part of usual type overload maintenance.

I am treating the stronger checks as regression, since it actually brings undesired behaviour changes as presented above.

主要言語
Go
スター
112k
フォーク
15.4k
平均マージ
3日 6時間
マージ済み PR(30日)
136

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

microsoft/TypeScript のほかの issue

microsoft/TypeScript の issue をすべて見る

似ている issue

Go の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。