-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Created ValueOf<T> util type (#1203)
resolves #1202
- Loading branch information
Showing
2 changed files
with
25 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |