microsoft / microsoft/TypeScript

Unsafe Map overload spoils direct property access on parenthesized assignment expression

Ouverte
#61,506 2 commentaires 2 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Domain: check: Type Inference Help Wanted Possible Improvement
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

🔎 Search Terms

map overload any unknown unsafe weakmap set binary assignment lazy init

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.0-dev.20250326#code/DYUwLgBAxghlAWIBcECyMAOAeAzmATgJYB2A5gDQQD2ARgFYhRgB8EAPhAK7EAmIAZiRA8A3ACgx-bk0JViEMCDwBGABQBrEAE8UeImQCUEAN5iI0OXgj4lnYJAC8EVbAQgIAfg9PiIAO5omKoGBgB0pOAa2gbi5gD0cebmAHoeZtbgnPjyNjh2YOIAvhJSxDJyCkpgAExROhB6JKRGpuauiJ7eEL4B6BjBsRbEVrn5EE7tIOGRmlox6QlJEKnpNmBZObb2RWJAA

💻 Code
let cache: Map<string, object> | undefined;

function test1(key: string) {
  const result = (cache ??= new Map()).get(key);
  //    ^? const result: any
  return result;
}

function test2(key: string) {
  cache ??= new Map();
  const result = cache.get(key);
  //    ^? const result: object | undefined
  return result;
}
🙁 Actual behavior

First result gets spoiled as any

🙂 Expected behavior

I'd expect both results to be object | undefined

Additional information about the issue

This bug reports an issue related to Map but, tbf, it's not unique to Map and a more general solution would have to be found to properly fix this. The problem is how checkBinaryLikeExpressionWorker create a union of left and right types here - without taking into consideration that the assignment target might already overlap with the right type. That union type gets reduced with UnionReduction.Subtype and .get gets read from it - instead of the assigned to reference on the left side.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Reproduisez la différence entre test1 et test2 à l’aide du TypeScript Playground lié, puis examinez checkBinaryLikeExpressionWorker, que le rapport identifie comme formant l’union des opérandes d’affectation. Suivez la manière dont cette union est réduite avant la résolution de .get. Le travail est considéré comme terminé lorsque l’accès à l’affectation entre parenthèses infère object | undefined comme la forme d’affectation séparée, avec une couverture de régression pour l’exemple signalé.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
45/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.