microsoft / microsoft/TypeScript

Utility type: Object entries tuple

Offen
#32,771 1 Kommentar 22 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Der Vorschlag zielt auf lib.es5.d.ts; beginne dort und vergleiche die vorhandenen Deklarationen von Utility Types mit der vorgeschlagenen Form Entries. Als erledigt gilt die Aufgabe, wenn der Utility Type konsistent mit der Standardbibliothek hinzugefügt wurde und die im Issue beschriebenen Beispiele für Tupel von Objekt-Einträgen unterstützt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.