enhance: Add tests for premature caching of contextual parameter types
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 2/5
- Temps estimé
- 1-3 heures
- Accessibilité débutants
- 78/100
- Type d'issue
- Bug
- Clarté
- Clairement spécifiée
- Activité
- Active
- Stack technique
- go, typescript
- Domaine
- compilers, testing-qa
Piste de recherche
Commencez par contextuallyTypedParametersWithInitializers2.ts et les cas existants d’initialiseurs de paramètres contextuels, puis exécutez les tests du compilateur associés. Ajoutez le cas proposé test4 et mettez à jour ses baselines de types et de symboles. Le travail est terminé lorsque le test réussit avec la protection de mise en cache actuelle et ne produit pas le diagnostic parasite TS2322.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.4k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de microsoft/TypeScript
-
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
microsoft/TypeScript#64322 · 2 commentaires · 1 réaction · 2 personnes assignées ·
-
Docs
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
microsoft/TypeScript#64118 · 1 commentaire ·
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 88/100
microsoft/TypeScript#64094 ·
-
Docs
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
microsoft/TypeScript#63959 · 5 commentaires ·
-
Domain: lib.d.ts Help Wanted
Difficulté 1/5 Moins d'une heure Accessibilité débutants 91/100
microsoft/TypeScript#63722 · 4 commentaires · 1 réaction ·
Toutes les issues de microsoft/TypeScript
Issues similaires
-
optimization optimization:agents-md-curator
Difficulté 2/5 1-3 heures Accessibilité débutants 86/100
githubnext/gh-aw-cao#13143 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 84/100
blinklabs-io/bursa#904 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 84/100
yanet-platform/ipfw-go#129 ·
-
bug confmap/provider/googlesecretmanagerprovider needs triage
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
open-telemetry/opentelemetry-collector-contrib#51273 · 2 commentaires ·
-
bug: AI Gateway client filter lists "Unknown" twice when NULL and literal Unknown clients coexist Ouvertebug
Difficulté 2/5 1-3 heures Accessibilité débutants 90/100