microsoft / microsoft/TypeScript

Allow usage of `Reflect.get()` by returning `unknown` instead of `any`

Aperta
#38,342 0 commenti 8 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Search Terms

Reflect get unknown

Suggestion

Change the type definition of Reflect.get() to return unknown instead of any.

Use Cases

Allow type safe dynamic access of fields on an object or { [k: string}: unknown }

Using Reflect.get(o, 'some key') and have it return unknown.

This allows usage of Reflect.get() in code bases where we do not want implicit any and where we have @typescript-eslint rules to ban unsafe usage of any.

Examples

/**
 * @param {CustomError} cause
 * @returns {{ [k: string]: unknown }}
 */
function getInfoForPlainError (cause) {
  /** @type {{ [k: string]: unknown }} */
  const info = {}
  for (const field of PLAIN_ERROR_FIELDS) {
    const v = reflectGet(cause, field)
    if (typeof v !== 'undefined') {
      info[field] = v
    }
  }
  return info
}

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Unfortunately changing a signature from any to unknown is a breaking change.

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 individuando la dichiarazione di lib.d.ts per Reflect.get e i test che verificano il tipo restituito inferito. Verifica in che modo la modifica di any in unknown influisce sulle aspettative esistenti relative al controllo dei tipi; il lavoro è completato quando la dichiarazione e i test pertinenti supportano in modo coerente il tipo restituito richiesto.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
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.