diff --git a/packages/utils/src/TypeUtils.test.ts b/packages/utils/src/TypeUtils.test.ts new file mode 100644 index 0000000000..729b0e2d77 --- /dev/null +++ b/packages/utils/src/TypeUtils.test.ts @@ -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[] = [1, 2, 3]; + + // No assertion since this is a types only test + }); +}); diff --git a/packages/utils/src/TypeUtils.ts b/packages/utils/src/TypeUtils.ts new file mode 100644 index 0000000000..143e6ba456 --- /dev/null +++ b/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; // 1 | 2 | 3 + * + * Instead of the more verbose: + * type A = typeof x[keyof typeof x]; // 1 | 2 | 3 + */ +export type ValueOf = T[keyof T];