microsoft / microsoft/TypeScript

Utility type: Object entries tuple

Abierto
#32,771 1 comentario 22 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

Utility type, object entries, tuple, mapped type

Suggestion

An Entries<T> utility type that constructs a union of all object entry tuples of type T in lib.es5.d.ts.

/**
 * Construct a union of all object entry tuples of type T
 */
type Entries<T> = {
    [P in keyof T]: [P, T[P]];
}[keyof T];

Use Cases

I understand the history behind why Object.entries provides string as the key type rather than the actual keys (because there may exist more actual keys that those defined in the type). I refer to discussion here.

Sometimes, however, we can be confident that only properties defined in the type will be present at runtime, and have found that a utility type like this can be helpful to more strictly define such types, particularly in tests.

Examples

const expected = {
   foo: "foo",
   bar: "Bar",
   baz: "BAZ",
} as const;

(Object.entries(expected) as Entries<typeof expected>).forEach(([input, output]) => {
  // where `myFunction`'s first parameter only accepts `"foo" | "bar" | "baz"` (or a superset)
  expect(myFunction(input)).to.equal(output);
});
function thatAcceptsJsxAttributeTuples(attributes: readonly Entries<JSX.IntrinsicElements["path"][]>): void {
  // ...
}

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.

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

La propuesta tiene como objetivo lib.es5.d.ts; empieza allí y compara las declaraciones existentes de utility types con la forma sugerida Entries. Se considera terminado cuando el utility type se haya añadido de forma coherente con la biblioteca estándar y admita los ejemplos de tuplas de entradas de objeto descritos en el issue.

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
Bien especificado
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.