Type Alias LiteralsRecord<T, U>

LiteralsRecord<T, U>: Record<Literals<T>, U>

Creates a new Record type from the literal properties of a type T, assigning their values the to the type U.

This can be useful for creating interfaces from the keys of an enum so that the keys are available at runtime for meta-programming purposes, while both tying the properties of the generated type to the enum keys and remaining as DRY as possible.

enum QUERY_KEY { id, name, created, updated }
// type of QUERY_KEY -> {
// [x: number]: number;
// readonly id: number;
// readonly name: number;
// readonly created: number;
// readonly updated: number;
// }
interface QueryRecord extends LiteralsRecord<typeof QUERY_KEY, string> { }
// type of QueryRecord -> {
// readonly id: string;
// readonly name: string;
// readonly created: string;
// readonly updated: string;
// }
// And for an interface with writable properties, use the following:
interface QueryRecord extends ReadWrite<LiteralsRecord<typeof QUERY_KEY, string>> { }
// type of QueryRecord -> {
// id: string;
// name: string;
// created: string;
// updated: string;
// }

Type Parameters

  • T
  • U