Skip to content

Commit 19fcf0e

Browse files
authored
feat: Created ValueOf<T> util type (#1203)
resolves #1202
1 parent de9b751 commit 19fcf0e

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ValueOf } from './TypeUtils';
2+
3+
describe('ValueOf', () => {
4+
it('should derive the value type', () => {
5+
const x = { a: 1, b: 2, c: 3 } as const;
6+
7+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
8+
const y: ValueOf<typeof x>[] = [1, 2, 3];
9+
10+
// No assertion since this is a types only test
11+
});
12+
});

packages/utils/src/TypeUtils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Util type to extract the value from an object.
3+
*
4+
* e.g. Given
5+
* declare const x: { a: 1; b: 2; c: 3 };
6+
*
7+
* The value type can be extracted like this:
8+
* type A = ValueOf<typeof x>; // 1 | 2 | 3
9+
*
10+
* Instead of the more verbose:
11+
* type A = typeof x[keyof typeof x]; // 1 | 2 | 3
12+
*/
13+
export type ValueOf<T> = T[keyof T];

0 commit comments

Comments
 (0)