microsoft / microsoft/TypeScript
ES6 Class: `asserts this is Type` raises error when variable containing instance is type narrowed
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
Bug Report
🔎 Search Terms
- Error: "Assertions require every name in the call target to be declared with an explicit type annotation"
- Assertation function as class method
- Type narrowing class and then calling assertation method
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about this
⏯ Playground Link
Playground link with relevant code
💻 Code
class MyClass {
public assertIsMyClass(): asserts this is MyClass {
if (!(this instanceof MyClass)) {
throw new Error('this was not of type MyClass');
}
}
}
const maybeMyClass: MyClass | undefined = new MyClass();
if (maybeMyClass) {
// This fails with error:
// Assertions require every name in the call target to be declared with an explicit type annotation.(2775)
maybeMyClass.assertIsMyClass();
// But if we simply assign to a new variable without the optionality, it's fine:
// this line works because we have narrowed the type through the if statement above
const definitelyMyClass: MyClass = maybeMyClass;
// This raises no error even though it's equivalent to the first attempt
definitelyMyClass.assertIsMyClass();
}
🙁 Actual behavior
Calling the class method assertation function raises error: "Assertions require every name in the call target to be declared with an explicit type annotation.(2775)"
🙂 Expected behavior
It should pass validation and narrow to the type given in the assertation.
This is because it will have this behavior if you simply assign to a new variable with a new type annotation (but which is equivalent to the narrowed type at that point).
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
提供された Playground リンクから始め、コードサンプルで診断を再現し、絞り込まれた変数と明示的に注釈付けされた変数を比較します。TypeScript コンパイラーでのアサーション呼び出しの検証を追跡し、このクラスメソッドのケースに対するカバレッジを追加します。絞り込まれた呼び出しが検証を通過し、既存のアサーションチェックが引き続き正しく機能すれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100