microsoft / microsoft/TypeScript
Utility type: Object entries tuple
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
La proposta riguarda lib.es5.d.ts; inizia da lì e confronta le dichiarazioni esistenti dei utility type con la forma suggerita Entries. Il lavoro è completo quando il utility type è stato aggiunto in modo coerente con la libreria standard e supporta gli esempi di tuple di entry dell’oggetto descritti nell’issue.
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
- Specificata chiaramente
- Idoneità per principianti
- 35/100