microsoft / microsoft/TypeScript

Only number index type is inferred from `any` computed property key

Aperta
#45,629 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.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

🔎 Search Terms
  • Computed properties
  • Indexers
  • Index signatures
🕗 Version & Regression Information
  • This is the behavior in every version I tried.
⏯ Playground Link

Playground link with relevant code

💻 Code
declare let a: any;

const obj = {
  [a]: "s",
}; // { [x: number]: string }

obj["abc"]; // error
🙁 Actual behavior

A numeric indexer is inferred. String index access marked as invalid.

🙂 Expected behavior

A string indexer is expected. No error for string access.

declare let a: any;

const obj = {
  [a]: "s",
}; // { [x: number]: string }

obj["abc"]; // no error

I created a small PR that addresses the issue as an example but I'm still not sure if the current behavior is intentional or not.

EDIT: I'm still not sure if the inferred indexer should have a String type, perhaps a union of the allowed primitive types fits better, e.g.:

declare let a: any;

const obj = {
  [a]: "s",
}; // { [x: string | number | symbol]: 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 traccia l’inferenza di proprietà calcolate/indexer per una chiave any. Determina il tipo di indexer inferito previsto, quindi verifica il risultato rispetto all’esempio mostrato di accesso tramite stringa e aggiungi o aggiorna la copertura nei test di verifica dei tipi pertinenti.

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
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.