microsoft / microsoft/TypeScript

Fail to infer Indexed access to the intersection type when it includes an union derived from a generic input

Offen
#59,946 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Domain: Indexed Access Types Help Wanted Possible Improvement
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔎 Search Terms

"indexed access on union", "generic index"

🕗 Version & Regression Information

5.6.2, and it seems never worked for every ts versions.

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.6.2#code/C4TwDgpgBAKhDOwA8MoQB7AgOwCbykQCcBLbAcwD4oBeKAbwG0BpKM2AXQC4GBrCED2YBfYYxgcoAMgbAAhkXIRgPYmXLCAUJoDGAe2yIoAMz17aUFGkw58hYKQqUAFAvI84iFJQCUtavSaUMFQ+obAUABucgA2AK4QHgjIMNR0bgDcQSFhRtHxEABMSV6pjADk-CDlkumKFVU1WSGhBnmxCQDMJSmUFfKKyjUWbv1uQxwZUAD001AAokREekSawkA

💻 Code
type Test<T extends string> = {[K in T]: {key: K}}[T] & {target: string}

const foo = <T extends string>(arg: Test<T>) => {
    const value: Test<T> = arg; 
    const value2: Test<T>['key'] = arg['key']; 
    const value3: Test<T>['target'] = arg['target']; // Error
}
🙁 Actual behavior

The declaration of value and value2 works well, but ts emit an error saying Type 'string' is not assignable to type '{ [K in T]: { key: K; }; }[T]["target"] & string' for value3.

🙂 Expected behavior

The Test<T>['target'] should equal to string and assume the type of '{ [K in T]: { key: K; }; }[T]["target"] as any before intersection, as it does when I give non-generic type in the same place. It seems even weird because it well infers the type of Test<T>['key'].

Additional information about the issue

With additional experiment, I found that it always happens when I generate an union type with generic input. For instance,

type Test<T> = (T extends string ? {key: number} : {key: string}) & {target: string}

const foo = <T extends string>(arg: Test<T>) => {
    const value: Test<T> = arg;
    const value2: Test<T>['key'] = arg['key'];
    const value3: Test<T>['target'] = arg['target']; // Error
}

it also does not work in the case above

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 verlinkten TypeScript Playground-Reproduktion und vergleiche die im Issue gezeigten generischen und nicht-generischen Indexed-Access-Fälle. Als abgeschlossen gilt die Änderung, wenn der generische Indexed Access für den Intersection-Typ den String-Wert für die Ziel-Property akzeptiert, ohne den gemeldeten Typfehler zu erzeugen, während die bestehende Key-Inferenz korrekt bleibt.

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
35/100

Neue Issues direkt in Ihr Postfach

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