microsoft / microsoft/TypeScript
Check for missing property on union type causes failure in subsequent property checks
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔎 Search Terms
"missing property", "property check", "type union", "control flow analysis"
🕗 Version & Regression Information
- This is the behavior in every version I tried*, and I reviewed the FAQ for entries about property checks
*There was a minor change between 4.8 and 4.9 that changed the types but did not change the behavior: inference improved from never to incorrectUnionElement & Record<"key", unknown>.
⏯ Playground Link
💻 Code
interface comp1 {
readonly a: number;
readonly b: number;
}
interface comp2 {
readonly a: number;
}
type comp =
| comp1
| comp2
function do_something() {
const comp = {} as comp;
if ("a" in comp) {
comp.a.toString();
// ^? comp
}
if ("b" in comp) {
comp.b.toString();
// ^? comp1
}
if ("c" in comp) {
comp.c.toString();
// ^? comp & Record<"c", unknown>
}
if ("a" in comp) {
comp.a.toString();
// ^? comp2
}
if ("b" in comp) {
comp.b.toString();
// ^? comp2 & Record<"b", unknown>
}
}
🙁 Actual behavior
Every property check after the ("c" in comp) one fails to narrow correctly if the key is one that is only defined for some of the element types of the type union. Attempting to access the property after one of these checks results in unknown. As well, the narrowed variable in subsequent conditionals is an arbitrary(?) union element.
🙂 Expected behavior
The conditionals that come after the ("c" in comp) conditional should have the same behavior as the ones before it, as they are the exact same code.
Additional information about the issue
This was initially noticed in a more complicated context with assertions, that I can include here as a secondary example. This one is even more strange, as it is a check for a property that is only defined on some of the elements of the union and it blocks its own duplicate check later.
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
Beginne mit dem bereitgestellten TypeScript Playground-Repro und vergleiche das Union-Narrowing vor und nach der Prüfung "c" in comp. Verfolge die Control-Flow-Analyse und die Behandlung von Property-Prüfungen, die beim Narrowing von Union-Typen beteiligt sind. Als erledigt gilt die Aufgabe, wenn spätere Prüfungen genauso narrowen wie identische Prüfungen vor dem Test auf die fehlende Property, einschließlich des sekundären Beispiels.
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
- Klar beschrieben
- Anfängerfreundlichkeit
- 35/100