Repository navigation
Expand file tree
/
Copy pathtype-asserted-object.ts
More file actions
54 lines (51 loc) · 1.97 KB
/
Copy pathtype-asserted-object.ts
File metadata and controls
54 lines (51 loc) · 1.97 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Provides common `Object.` functions with additional type assertions.
* To do this, it is assumed that the provided object doesn't have more properties (at runtime) than specified in its type.
* Based on how the TS type system works, **this assumption cannot be made for every Object** -> the default typings are very weak.
* @See https://github.com/microsoft/TypeScript/pull/12253#issuecomment-263132208 and https://stackoverflow.com/questions/55012174/why-doesnt-object-keys-return-a-keyof-type-in-typescript
*/
export namespace TypeAssertedObject {
/**
* {@link Object.entries} with additional type assertions
* See {@link TypeAssertedObject}
* @returns an array of key/values of the enumerable properties of an object
*/
export function entries<T extends { [key: string]: any }>(
object: T
): Exclude<
{
[Key in keyof T]: [Key, T[Key]];
}[keyof T],
undefined
>[] {
return Object.entries(object) as any;
}
/**
* {@link Object.values} with additional type assertions
* See {@link TypeAssertedObject}
* @returns an array of values of the enumerable properties of an object
*/
export function values<T extends { [key: string]: any }>(
object: T
): T[keyof T][] {
return Object.values(object);
}
/**
* {@link Object.keys} with additional type assertions
* See {@link TypeAssertedObject}
* @returns an array of keys of the enumerable properties of an object
*/
export function keys<T extends { [key: string]: any }>(
object: T
): (keyof T)[] {
return Object.keys(object);
}
/**
* {@link Object.fromEntries} with additional type assertions
* See {@link TypeAssertedObject}
* @returns an object from an array of key/value pairs
*/
export function fromEntries<K extends string, V>(objectEntries: [K, V][]) {
return Object.fromEntries(objectEntries) as { [key in K]?: V };
}
}