Skip to content

Commit 4c695a2

Browse files
committed
types derived from RequestSpec can be more generic
1 parent cd1d6fe commit 4c695a2

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

src/RequestSpec.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,14 @@ export type GraphType = 'single_metric' | 'predefined_graph';
3636

3737
export type Aggregation = 'off' | 'sum' | 'average' | 'minimum' | 'maximum';
3838

39-
export type RequestSpecStringKeys = 'host_name' | 'site' | 'service' | 'graph' | 'graph_type' | 'aggregation';
39+
export type PickByValue<T, V> = Pick<T, { [K in keyof T]: T[K] extends V ? K : never }[keyof T]>;
4040

41-
export type RequestSpecNegatableOptionKeys = 'host_name_regex' | 'service_regex' | 'host_in_group' | 'service_in_group';
41+
export type RequestSpecStringKeys = keyof PickByValue<RequestSpec, string | undefined>;
42+
43+
export type RequestSpecNegatableOptionKeys = keyof PickByValue<RequestSpec, NegatableOption | undefined>;
4244

4345
export const defaultRequestSpec: Partial<RequestSpec> = {
4446
aggregation: 'off',
4547
graph_type: 'predefined_graph',
4648
};
47-
export type FilterEditorKeys =
48-
| 'site'
49-
| 'host_name'
50-
| 'host_name_regex'
51-
| 'host_in_group'
52-
| 'host_labels'
53-
| 'host_tags'
54-
| 'service'
55-
| 'service_regex'
56-
| 'service_in_group';
49+
export type FilterEditorKeys = Exclude<keyof RequestSpec, 'graph_type' | 'aggregation' | 'graph'>;

tests/unit/RequestSpec.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { PickByValue } from '../../src/RequestSpec';
2+
3+
type Test = PickByValue<{ foo: string; bar: number }, string>;
4+
const value: Test = { foo: '' };
5+
6+
describe('we have no unit tests yet', () => {
7+
it('but want just make sure PickByValue works as expected', () => {
8+
expect(1).toStrictEqual(1);
9+
});
10+
});

0 commit comments

Comments
 (0)