microsoft / microsoft/TypeScript
Assertion function methods complains about missing annotation even with explicit annotation
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
This may be working as intended but the error message is at least unclear for this case. It's not obvious why explicit annotation with a Union is different than explicit annotation with a class type.
TypeScript Version: 3.9.4
Search Terms:
- assertion function union
- assertion function
Code
type Pet = Cat | Dog;
type Filter<V extends Pet, T extends 'dog' | 'cat' > = V extends { type: T } ? V : never
abstract class Base {
abstract type: 'dog' | 'cat'
assert<T extends 'dog' | 'cat'>(this: Pet, type: T): asserts this is Filter<Pet, T> {
if (this.type === type) return
throw new Error('nope')
}
}
class Dog extends Base {
type = 'dog' as const
bark() { }
}
class Cat extends Base {
type = 'cat' as const
meow() { }
}
const a: Pet = new Dog() as any
const b: Dog = new Dog() as any
a.assert('dog')
a.bark()
b.assert('dog')
b.bark()
Expected behavior:
a.assert() to narrow the type and not complain about an explicit type annotation
Actual behavior:
a.asserts is marked as implicitly typed
Playground Link:
Related Issues:
#36931
#37241
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされた TypeScript Playground で、提供されている Pet、Base、Dog、Cat の例を使って報告された動作を再現します。まず、コンパイラーがアサーションメソッドの明示的な asserts this is Filter<Pet, T> アノテーションをどのように検査するかを追跡し、次にクラス型の場合と比較します。ユニオンアノテーションが一貫して処理されるか、拒否される理由を診断が明確に説明し、関連するコンパイラーテストにリグレッションカバレッジが追加されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100