Last change
on this file since 84d0fbb was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
829 bytes
|
Line | |
---|
1 | /**
|
---|
2 | Create a union of the given object's values, and optionally specify which keys to get the values from.
|
---|
3 |
|
---|
4 | Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/31438) if you want to have this type as a built-in in TypeScript.
|
---|
5 |
|
---|
6 | @example
|
---|
7 | ```
|
---|
8 | // data.json
|
---|
9 | {
|
---|
10 | 'foo': 1,
|
---|
11 | 'bar': 2,
|
---|
12 | 'biz': 3
|
---|
13 | }
|
---|
14 |
|
---|
15 | // main.ts
|
---|
16 | import {ValueOf} from 'type-fest';
|
---|
17 | import data = require('./data.json');
|
---|
18 |
|
---|
19 | export function getData(name: string): ValueOf<typeof data> {
|
---|
20 | return data[name];
|
---|
21 | }
|
---|
22 |
|
---|
23 | export function onlyBar(name: string): ValueOf<typeof data, 'bar'> {
|
---|
24 | return data[name];
|
---|
25 | }
|
---|
26 |
|
---|
27 | // file.ts
|
---|
28 | import {getData, onlyBar} from './main';
|
---|
29 |
|
---|
30 | getData('foo');
|
---|
31 | //=> 1
|
---|
32 |
|
---|
33 | onlyBar('foo');
|
---|
34 | //=> TypeError ...
|
---|
35 |
|
---|
36 | onlyBar('bar');
|
---|
37 | //=> 2
|
---|
38 | ```
|
---|
39 | */
|
---|
40 | export type ValueOf<ObjectType, ValueType extends keyof ObjectType = keyof ObjectType> = ObjectType[ValueType];
|
---|
Note:
See
TracBrowser
for help on using the repository browser.