microsoft / microsoft/TypeScript
`asserts this is T` does not narrow type of `this`
オープン
まだ誰も着手していません。
Cursed?
Domain: This-Typing
Possible Improvement
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
🔎 Search Terms
asserts this is Tnarrowassertions- I also checked #59707, but it is unrelated (generic types).
🕗 Version & Regression Information
- Playground:
v5.7.0-dev.20241004 - At least since
v5.6.2
⏯ Playground Link
💻 Code
class A {
onlyDefinedInA() {
console.log("A");
}
assertA(): asserts this is A { }
}
class B {
assertA(): asserts this is A { }
}
type C = A | B;
function assertA(c: C): asserts c is A {
}
function test1(c: C) {
c.assertA();
c.onlyDefinedInA(); // Error: Property 'onlyDefinedInA' does not exist on type 'C'. Property 'onlyDefinedInA' does not exist on type 'B'.
}
function test2(c: C) {
assertA(c);
c.onlyDefinedInA(); // Works!
}
🙁 Actual behavior
Type of c is not narrowed after the method call, producing an error.
🙂 Expected behavior
I expect the member function to to perform type narrowing.
Additional information about the issue
No response
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされた TypeScript Playground の再現コードから始め、test1 のメソッド呼び出しのケースと test2 のスタンドアロンのアサーションを比較します。メンバー呼び出し後に asserts this is A がどのように処理されるかを追跡し、次に、メソッド呼び出しによって c が絞り込まれ、スタンドアロンのケースを後退させることなく onlyDefinedInA() が受け入れられることを確認します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 静か
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 48/100