-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathobject-values.ts
More file actions
30 lines (22 loc) · 1.05 KB
/
object-values.ts
File metadata and controls
30 lines (22 loc) · 1.05 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
import type {ArrayEntryValue} from './internal-types.js';
/**
A strongly-typed version of `Object.values()`.
This is useful since `Object.values()` always returns `T[]`. This function returns a strongly-typed array of the values of the given object.
- [TypeScript issues about this](https://github.com/microsoft/TypeScript/pull/12253)
@example
```
import {objectValues} from 'ts-extras';
const object: {a: number; b?: string} = {a: 1, b: 'hello'};
const stronglyTypedValues = objectValues(object);
//=> Array<number | string>
const untypedValues = Object.values(object);
//=> Array<string | number | undefined>
```
@category Improved builtin
*/
// eslint-disable-next-line @typescript-eslint/no-restricted-types -- We intentionally use `object` to accept interfaces.
// Intentionally unsafe cast to provide strongly-typed Object.values().
export const objectValues = Object.values as {
<Type extends readonly unknown[]>(value: Type): Array<ArrayEntryValue<Type>>;
<Type extends object>(value: Type): Array<Required<Type>[Extract<keyof Type, string | number>]>;
};