We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent de9b751 commit 19fcf0eCopy full SHA for 19fcf0e
2 files changed
packages/utils/src/TypeUtils.test.ts
@@ -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
@@ -0,0 +1,13 @@
+/**
+ * Util type to extract the value from an object.
+ *
+ * e.g. Given
+ * declare const x: { a: 1; b: 2; c: 3 };
+ * The value type can be extracted like this:
+ * type A = ValueOf<typeof x>; // 1 | 2 | 3
+ * Instead of the more verbose:
+ * type A = typeof x[keyof typeof x]; // 1 | 2 | 3
+ */
13
+export type ValueOf<T> = T[keyof T];
0 commit comments