enhance: Add tests for premature caching of contextual parameter types
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 78/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- go, typescript
- 領域
- compilers, testing-qa
調査の方向性
contextuallyTypedParametersWithInitializers2.ts と既存のコンテキスト付きパラメーター初期化子のケースから始め、関連するコンパイラーテストを実行します。提案された test4 ケースを追加し、その型およびシンボルのベースラインを更新します。現在のキャッシュガードでテストがパスし、誤って生成される TS2322 診断が発生しなければ完了です。
索引モデルが issue の本文から書いたものです。
説明
Acknowledgement
- I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.
Comment
Add tests for premature caching of contextual parameter types
The checker currently handles the following code correctly.
But the existing cases in contextuallyTypedParametersWithInitializers2.ts do not appear to cover the following case: the combination of premature type caching and parameter whose type is widened during contextual checking.
Example:
It is ok:
const fn: (reader: () => number, value: 1) => void =
(get = () => x, x = 0) => {
x.toFixed();
};
Why this needs regression coverage:
when caching the symbol type, we need to check if it is context sensitive, as the author intentionly said:
func (c *Checker) getTypeOfVariableOrParameterOrProperty(symbol *ast.Symbol) *Type {
links := c.valueSymbolLinks.Get(symbol)
if links.resolvedType == nil {
t := c.getTypeOfVariableOrParameterOrPropertyWorker(symbol)
if t == nil {
panic("Unexpected nil type")
}
// For a contextually typed parameter it is possible that a type has already
// been assigned (in assignTypeToParameterAndFixTypeParameters), and we want
// to preserve this type. In fact, we need to _prefer_ that type, but it won't
// be assigned until contextual typing is complete, so we need to defer in
// cases where contextual typing may take place.
if links.resolvedType == nil && !c.isParameterOfContextSensitiveSignature(symbol) {
links.resolvedType = t
}
return t
}
return links.resolvedType
}
Checking get = () => x queries x before its contextual parameter checking is complete:
- The initial query get the contextual type
1. - Later, checking the default value
0and widens the parameter's type tonumber. - If the initial result
1is cached,assignParameterTypereturns early and cannot assign the final type.
This would incorrectly produce the error:
Type '0' is not assignable to type '1'.
The existing guard in getTypeOfVariableOrParameterOrProperty prevents this premature caching:
!c.isParameterOfContextSensitiveSignature(symbol)
The example passes with the current implementation. Removing the guard will produce the incorrect TS2322 diagnostic.
Proposed change:
Just add this case to contextuallyTypedParametersWithInitializers2.ts
const test4: (reader: () => number, value: 1) => void =
(get = () => x, x = 0) => {
x.toFixed();
};
Add this case to the contextual parameter initializer tests, together with type and symbol baselines, to protect the existing behavior against regressions.
Existing tests cover the caching guard’s effect on implicit any diagnostics. This test adds coverage of type widening for parameters with default initializers, ensuring that prematurely cached literal types do not cause spurious errors.
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
microsoft/TypeScript のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
microsoft/TypeScript#64322 · コメント 2 件 · リアクション 1 件 · 担当者 2 名 ·
-
Docs
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
microsoft/TypeScript#64118 · コメント 1 件 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
microsoft/TypeScript#64094 ·
-
Docs
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
microsoft/TypeScript#63959 · コメント 5 件 ·
-
Domain: lib.d.ts Help Wanted
難易度 1/5 1時間未満 初心者へのやさしさ 91/100
microsoft/TypeScript#63722 · コメント 4 件 · リアクション 1 件 ·
microsoft/TypeScript の issue をすべて見る
似ている issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 92/100
milvus-io/birdwatcher#545 ·
-
kind/bug
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
kubernetes-sigs/prow#953 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
caddyserver/caddy#8046 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
jaegertracing/jaeger#9588 ·