Skip to content

Commit

Permalink
feat: Created ValueOf<T> util type (#1203)
Browse files Browse the repository at this point in the history
resolves #1202
  • Loading branch information
bmingles authored Apr 4, 2023
1 parent de9b751 commit 19fcf0e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/utils/src/TypeUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ValueOf } from './TypeUtils';

describe('ValueOf', () => {
it('should derive the value type', () => {
const x = { a: 1, b: 2, c: 3 } as const;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const y: ValueOf<typeof x>[] = [1, 2, 3];

// No assertion since this is a types only test
});
});
13 changes: 13 additions & 0 deletions packages/utils/src/TypeUtils.ts
Original file line number Diff line number Diff line change
@@ -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
*/
export type ValueOf<T> = T[keyof T];

0 comments on commit 19fcf0e

Please sign in to comment.