microsoft / microsoft/TypeScript

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

Abierto
#38,342 0 comentarios 8 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Awaiting More Feedback Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza localizando la declaración de lib.d.ts para Reflect.get y las pruebas que cubren su tipo de retorno inferido. Comprueba cómo afecta cambiar any por unknown a las expectativas existentes de comprobación de tipos; el trabajo estará terminado cuando la declaración y las pruebas relevantes admitan de forma coherente el tipo de retorno solicitado.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Nueva funcionalidad
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.