microsoft / microsoft/TypeScript

Narrow object property indexed by another object's property of unique symbol type (like `Symbol.iterator`)

Offen
#63,059 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

🔍 Search Terms

type guard, narrowing, control flow analysis, property, index, known type, symbol type, bracket notation, unique symbol, known symbol, Symbol.iterator, #10530

✅ Viability Checklist
⭐ Suggestion

This is yet another version of #10530 that doesn't seem to be covered by cases in #56389 or #61176. It might even be covered by #62247 and/or #62314 but I'm not sure. TypeScript already narrows by bracket notation when the key is a unique symbol variable:

declare const s: unique symbol;
declare const x: {}
if ((s in x) && (typeof x[s] === "string")) { x[s].toUpperCase(); }; // okay

but this no longer works if the key is a unique symbol property of another object:

declare const obj: { readonly s: unique symbol }
declare const x: {}
if ((obj.s in x) && (typeof x[obj.s] === "string")) { x[obj.s].toUpperCase(); } // error

The suggestion here is to make the latter work like the former.

📃 Motivating Example

See https://stackoverflow.com/questions/79877994/why-does-my-test-for-an-iterableiterator-fail

Consider

function f(x: object) {
    if ((Symbol.iterator in x) && (typeof x[Symbol.iterator] === "function")) {
        x[Symbol.iterator].length; // error
        //~~~~~~~~~~~~~~~~ <-- Object is of type 'unknown'.(2571)
    }
}

This just looks like a somewhat unfortunate gap in type guarding, where TS knows Symbol.iterator is a unique symbol but won't actually do the narrowing unless you put that unique symbol in its own variable directly:

function f(x: object) {
    const symbolIterator: typeof Symbol.iterator = Symbol.iterator;
    if ((symbolIterator in x) && (typeof x[symbolIterator] === "function")) {
        x[symbolIterator].length; // okay
    }
}

Playground link

💻 Use Cases
  1. What do you want to use this for? To allow people to type guard on Symbol.XXX properties
  2. What shortcomings exist with current approaches? People apparently don't like to copy things to new variables just for narrowing.
  3. What workarounds are you using in the meantime? Copy things to new variables. The obvious approach is the old standby, where we replace if (f(obj[k])) g(obj[k]) with const objK = obj[k]; if (f(objK)) g(objK)

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der Lektüre von TypeScripts Kontrollflussanalyse und der Behandlung von Typwächtern für die Klammernotation und vergleiche dabei das funktionierende Beispiel mit einer unique-symbol-Variable mit den fehlschlagenden Fällen obj.s und Symbol.iterator. Sieh dir die zugehörigen Issues #10530, #56389, #61176, #62247 und #62314 an und verwende anschließend das bereitgestellte Playground-Beispiel, um zu überprüfen, dass der Eigenschaftszugriff nach Abschluss der Änderung konsistent eingegrenzt wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.