enhance: Add tests for premature caching of contextual parameter types
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 78/100
- Tipo de issue
- Error
- Claridad
- Bien especificado
- Estado de actividad
- Activo
- Stack tecnológico
- go, typescript
- Área
- compilers, testing-qa
Línea de trabajo
Comienza con contextuallyTypedParametersWithInitializers2.ts y los casos existentes de inicializadores de parámetros contextuales, y después ejecuta las pruebas del compilador asociadas. Añade el caso propuesto test4 y actualiza sus baselines de tipos y símbolos. Se considera completado cuando la prueba pasa con la protección de caché actual y no produce el diagnóstico espurio TS2322.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 15 h
- PR fusionados (30 d)
- 106
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de microsoft/TypeScript
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
microsoft/TypeScript#64322 · 2 comentarios · 1 reacción · 2 asignados ·
-
Docs
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
microsoft/TypeScript#64118 · 1 comentario ·
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 88/100
microsoft/TypeScript#64094 ·
-
Docs
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
microsoft/TypeScript#63959 · 5 comentarios ·
-
Domain: lib.d.ts Help Wanted
Dificultad 1/5 Menos de una hora Aptitud para principiantes 91/100
microsoft/TypeScript#63722 · 4 comentarios · 1 reacción ·
Todos los issues de microsoft/TypeScript
Issues similares
-
Type/Bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
OpenNSW/nsw-srilanka#497 ·
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 92/100
milvus-io/birdwatcher#545 ·
-
kind/bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
kubernetes-sigs/prow#953 · 1 comentario ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
caddyserver/caddy#8046 ·