microsoft / microsoft/TypeScript
Unsafe Map overload spoils direct property access on parenthesized assignment expression
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
🔎 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
💻 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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Reproduziere den Unterschied zwischen test1 und test2 mithilfe des verknüpften TypeScript Playground und untersuche anschließend checkBinaryLikeExpressionWorker, das laut Bericht die Vereinigung der Zuweisungsoperanden bildet. Verfolge, wie diese Vereinigung reduziert wird, bevor .get aufgelöst wird. Als erledigt gilt die Aufgabe, wenn der Zugriff auf die eingeklammerte Zuweisung wie die separate Zuweisungsform object | undefined ableitet, mit einer Regressionstestabdeckung für das gemeldete Beispiel.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100