enhance: Add tests for premature caching of contextual parameter types

未关闭 适合新手
#64,278 1 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
go, typescript

调研方向

从 contextuallyTypedParametersWithInitializers2.ts 和现有的上下文参数初始化器用例开始,然后运行相关的编译器测试。添加提议的 test4 用例,并更新其类型和符号基线。完成的标准是测试在当前缓存 guard 下通过,并且不会产生多余的 TS2322 诊断。

由索引模型根据 Issue 内容生成。

描述

Possible Improvement
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:

  1. The initial query get the contextual type 1.
  2. Later, checking the default value 0 and widens the parameter's type to number.
  3. If the initial result 1 is cached, assignParameterType returns 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 小时
30 天内合并 PR
117

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/TypeScript 的其他 Issue

查看 microsoft/TypeScript 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。