[Edit]
+
0
-
0

TypeScript - create new Record<Key, Value>() using constructor (new operator)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
// ------------------------------------------------------------------ // record.tsx file: // ------------------------------------------------------------------ export interface RecordConstructor { <K extends keyof any, V>(object?: Record<K, V>): Record<K, V>; new <K extends keyof any, V>(object?: Record<K, V>): Record<K, V>; } export const Record: RecordConstructor = Object as RecordConstructor; // ------------------------------------------------------------------ // example.tsx file: // ------------------------------------------------------------------ import {Record} from './record.tsx'; const r1 = Record<string, number>(); const r2 = new Record<string, number>(); const r3 = Record<string, number>({id: 1}); const r4 = new Record<string, number>({id: 1}); const r5: Record<string, number> = {id: 1}; const r6: Record<string, number> = Record<string, number>({id: 1}); const r7: Record<string, number> = new Record<string, number>({id: 1}); // ------------------------------------------------------------------ // See also: // // 1. https://dirask.com/snippets/TypeScript-create-new-Record-Key-Value-using-constructor-new-operator-DnvYbD