microsoft / microsoft/TypeScript

Can't constrain a generic parameter to have string keys?

Offen
#43,771 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: Index Types
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

Bug Report

Search Terms: Type 'X' cannot be used to index type 'Y', constrain generic parameter to have string keys

  • This is the behavior in every version I tried
Code

Playground link

This type is meant to map another type's values to arrays of {message: V} but also allow {message:unknown} for other string keys.

type Example<M> =
  { [K in keyof M]?: {message: M[K]}[] } &
  { [key in string]: {message: unknown}[] };

For example, the intent is that Example<{ foo: string }> should become

{
  foo: {message: string}[];
  [key: string]: {message: unknown}[];
}

And this works: we know that "foo" and other strings are valid keys.

const obj: Example<{foo: string}> = {};
obj["foo"] = [{message: "hi"}];
obj["bar"] = [{message: "x"}];

But trying to use Example with a generic parameter gives errors. Even if we don't know M["foo"], we should know that keyof M is string.

// attempt to constrain M's keys to be strings
function test<M extends {[key in string]: unknown}>() {
  const obj: Example<M> = {};
  obj["foo"] = [];  // error: Type '"foo"' cannot be used to index type 'Example<M>'.
  obj["bar"] = [];  // error: Type '"foo"' cannot be used to index type 'Example<M>'.
}
// should fail, but doesn't -- we've tried to constrain M to have string keys
test<{ 4: string }>();
🙁 Actual behavior

Example<M> cannot be indexed using string when M is a generic parameter.

🙂 Expected behavior

It should always be possible to index with string, since the definition of Example includes { [key in string]: ... }.

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 den konkreten Fall Example<{ foo: string }> mit dem generischen Fall test<M extends { [key in string]: unknown }>(). Untersuche das Verhalten von generischem Indexed Access und Schlüsselbeschränkungen; abgeschlossen ist die Untersuchung, wenn String-Indexierung für das generische Example funktioniert, während eine Instanziierung mit numerischem Schlüssel abgelehnt wird.

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

Neue Issues direkt in Ihr Postfach

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