[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 36 37 38 39 40 41 42
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>; } const Record: RecordConstructor = Object as RecordConstructor; // Usage example: const r1 = Record<string, number>(); const r2 = Record<string, number>({id: 1}); const r3 = new Record<string, number>(); 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}); // Constructor test: const source = {id: 1}; const object1 = new Object(); const object2 = new Object(source); const record1 = new Record<string, number>(); const record2 = new Record<string, number>(source); console.log(object1 === source); // false console.log(object2 === source); // true console.log(record1 === source); // false console.log(record2 === source); // true // ------------------------------------------------------------------ // See also: // // 1. https://dirask.com/snippets/TypeScript-create-new-Record-Key-Value-using-constructor-new-operator-jEwK9D