microsoft / microsoft/TypeScript
Assertion methods (`asserts this is`) are not CFA'd without error
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
TypeScript Version: 4.0.5, 4.1.0-beta, 4.2.0-dev.20201112
Search Terms:
- Multiple assertion methods
- "asserts this is"
- assertion "cfa"
Code
class X<T, Locked extends boolean> {
public x: null | T | (Locked extends true ? 1 : 2) = null
public assert<U>(): asserts this is X<U, Locked> { }
public lock(): asserts this is X<T, true> {}
}
const x: X<string | number, false> = new X<string | number, false>()
x.assert<string>()
// x is X<string, false> here
x.lock()
Expected behavior:
Either:
- After
x.lock(),xis narrowed toX<string, true> - Or, the
x.lock()line throws at compile-time due to not being CFA'd
Actual behavior:
After x.lock(), x is narrowed to X<string, false> & X<string | number, true>.
Analysis:
x being typed as X<string, false> & X<string | number, true> shows that the x.lock() narrows from the original type (X<string | number, false>) instead of the narrowed type at that position (X<string, false>).
If instead a "top-level" assertion function is used the type is properly narrowed:
class X<T, Locked extends boolean> {
public x: null | T | (Locked extends true ? 1 : 2) = null
public assert<U>(): asserts this is X<U, Locked> { }
public lock(): asserts this is X<T, true> {}
}
const x: X<string | number, false> = new X<string | number, false>()
x.assert<string>()
// x is X<string, false> here
declare function lock<T>(v: X<T, boolean>): asserts v is X<T, true>
lock(x)
This leads me to believe this is an issue with the x.lock() call not being CFA'd, in which case the correct behavior would be to throw ts(2775) on the x.lock() line.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、提供されている TypeScript Playground の再現コードを実行し、x.assert<string>() と x.lock() の後での制御フローの narrowing を比較してください。コンパイラーによる asserts this is メソッドの扱いと、報告される intersection type を調査してください。2 番目の assertion が X<string, true> に narrowing されるか、期待されるコンパイル時エラーが報告され、さらにこの例のリグレッションカバレッジが追加されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100