microsoft / microsoft/TypeScript
Error span incorrect when `this` constraint on member function fails
オープン
まだ誰も着手していません。
Domain: Related Error Spans
Help Wanted
Possible Improvement
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
🔎 Search Terms
- error span
this: never- this chain
- builder pattern
🕗 Version & Regression Information
- This is the behavior in every version I tried (including the latest 5.7.2)
⏯ Playground Link
💻 Code
/**
* Member function based case
*/
class FunctionBasedBuilder<Step extends number=1> {
step1(this: Step extends 1 ? this : never): FunctionBasedBuilder<2> {
return this as any;
}
step2(this: Step extends 2 ? this : never): FunctionBasedBuilder<3> {
return this as any;
}
// skip step 3 explicitly to demonstrate the error
step4(this: Step extends 4 ? this : never): FunctionBasedBuilder<5> {
return this as any;
}
}
/**
* Member variable based case
*/
class VariableBasedBuilder<Step extends number=1> {
step1: (Step extends 1 ? () => VariableBasedBuilder<2> : never) = (() => {
return this as any;
}) as any;
step2: (Step extends 2 ? () => VariableBasedBuilder<3> : never) = (() => {
return this as any;
}) as any;
// skip step 3 explicitly to demonstrate the error
step4: (Step extends 4 ? () => VariableBasedBuilder<5> : never) = (() => {
return this as any;
}) as any;
}
const funcBuilder = new FunctionBasedBuilder();
// all of `foo.step1().step2()` turns into an unhelpful error message
funcBuilder.step1().step2().step4();
const varBuilder = new VariableBasedBuilder();
// the error properly appears only on step 4
varBuilder.step1().step2().step4();
🙁 Actual behavior
In the function case, the entire span from foo.step1().step2() gets marked as an error instead of only step4() which is what actually caused the error
🙂 Expected behavior
Just like in the member variable case, it would be better if the error was shown on step4()
Additional information about the issue
I think this ends up being equivalent to #28159 so I'm not sure it can be solved without introducing the "curse" mentioned in #60140
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされた TypeScript Playground を開き、関数ベースと変数ベースの builder のケースを比較して、最後の step4 呼び出しに対する診断範囲に注目してください。issues #28159 と #60140 との関連性を確認してください。関数ベースのケースで、変数ベースのケースと同様に step4() だけがハイライトされ、報告されたエラーがリグレッションしなければ完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 30/100