microsoft / microsoft/TypeScript

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

Aperta
#43,771 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: Index Types
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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]: ... }.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con la riproduzione collegata in TypeScript Playground e confronta il caso concreto Example<{ foo: string }> con il caso generico test<M extends { [key in string]: unknown }>(). Analizza il comportamento dell’accesso indicizzato generico e dei vincoli sulle chiavi; il lavoro è completo quando l’indicizzazione con string funziona per l’Example generico, mentre un’instanziazione con una chiave numerica viene rifiutata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.