Skip to content

Commit 67bab3f

Browse files
committed
remove usage of generic default value
the original intent was to to make sure that Value is bound to RequestSpec[Key], but actually typescript was thinking of Value as completely untyped, although they way we use it currently it's alway `string` or `string | undefined`. by removing this default completely we don't longer need to cast value to string.
1 parent 4c695a2 commit 67bab3f

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

src/ui/components.tsx

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,21 @@ import {
2424
TagValue,
2525
} from '../RequestSpec';
2626

27-
interface CommonProps<Key extends keyof RequestSpec, Value = RequestSpec[Key]> {
27+
interface CommonProps<T> {
2828
label?: string;
29+
requestSpecKey?: string;
30+
onChange(value: T): void;
31+
value: T;
32+
}
33+
34+
interface CheckMkAsyncSelectProps<Key extends keyof RequestSpec> extends CommonProps<RequestSpec[Key]> {
35+
width?: number;
2936
requestSpecKey?: Key;
30-
onChange(value: Value): void;
31-
value: Value;
37+
autocompleter: (prefix: string) => Promise<Array<SelectableValue<NonNullable<RequestSpec[Key]>>>>;
38+
inputId: string; // make the InlineField magic do its work // TODO: find better solution
3239
}
3340

34-
interface CheckMkAsyncSelectProps<Key extends keyof RequestSpec, Value = RequestSpec[Key]>
35-
extends CommonProps<Key, Value> {
41+
interface CheckMkGenericAsyncSelectProps<Value> extends CommonProps<Value> {
3642
width?: number;
3743
autocompleter: (prefix: string) => Promise<Array<SelectableValue<NonNullable<Value>>>>;
3844
inputId: string; // make the InlineField magic do its work // TODO: find better solution
@@ -46,8 +52,8 @@ function findValueInOptions<Value>(lookupOptions: Array<NonNullable<SelectableVa
4652
return null;
4753
}
4854

49-
export const CheckMkAsyncSelect = function <Key extends keyof RequestSpec, Value = RequestSpec[Key]>(
50-
props: CheckMkAsyncSelectProps<Key, Value>
55+
export const CheckMkGenericAsyncSelect = function <Value extends string | (string | undefined)>(
56+
props: CheckMkGenericAsyncSelectProps<Value>
5157
) {
5258
const { autocompleter, width, value, onChange, label, inputId } = props;
5359
const [options, setOptions] = React.useState([] as Array<SelectableValue<Value>>);
@@ -80,7 +86,7 @@ export const CheckMkAsyncSelect = function <Key extends keyof RequestSpec, Value
8086
// this would mean we would display an error "could not find element 'xxx'"
8187
// in order to prevent that, we do an additional query, with the
8288
// value to make sure we receive the value (and label).
83-
const specificData = await autocompleter(value as string);
89+
const specificData = await autocompleter(value);
8490
data.push(...specificData);
8591
}
8692
setOptions(data);
@@ -123,10 +129,12 @@ export const CheckMkAsyncSelect = function <Key extends keyof RequestSpec, Value
123129
/>
124130
);
125131
};
132+
export const CheckMkAsyncSelect = function <Key extends RequestSpecStringKeys>(props: CheckMkAsyncSelectProps<Key>) {
133+
return CheckMkGenericAsyncSelect<RequestSpec[Key]>(props);
134+
};
126135

127-
interface CheckMkSelectProps<Key extends RequestSpecStringKeys, Value = RequestSpec[Key]>
128-
extends CommonProps<Key, Value> {
129-
autocompleter: (prefix: string) => Promise<Array<SelectableValue<NonNullable<Value>>>>;
136+
interface CheckMkSelectProps<Key extends RequestSpecStringKeys> extends CommonProps<RequestSpec[Key]> {
137+
autocompleter: (prefix: string) => Promise<Array<SelectableValue<NonNullable<RequestSpec[Key]>>>>;
130138
}
131139

132140
export const CheckMkSelect = <Key extends RequestSpecStringKeys>(props: CheckMkSelectProps<Key>) => {
@@ -197,7 +205,7 @@ export const CheckMkSelectNegatable = <T extends RequestSpecNegatableOptionKeys>
197205
);
198206
};
199207

200-
interface FilterProps<Key extends RequestSpecNegatableOptionKeys> extends CommonProps<Key> {
208+
interface FilterProps<Key extends RequestSpecNegatableOptionKeys> extends CommonProps<RequestSpec[Key]> {
201209
requestSpecKey: Key;
202210
}
203211

@@ -281,22 +289,22 @@ const SingleTag = (props: {
281289
return (
282290
<HorizontalGroup>
283291
<Label>Host tag {props.index}: </Label>
284-
<CheckMkAsyncSelect
292+
<CheckMkGenericAsyncSelect<string | undefined>
285293
onChange={(val) => {
286294
onChange({ ...value, group: val ?? '' });
287295
}}
288296
value={value.group}
289297
inputId={'group'}
290298
autocompleter={groupAutocompleter}
291299
/>
292-
<CheckMkAsyncSelect
300+
<CheckMkGenericAsyncSelect<string | undefined>
293301
width={8}
294302
onChange={(val) => onChange({ ...value, operator: val ?? 'is' })}
295303
value={value.operator}
296304
autocompleter={operatorAutocompleter}
297305
inputId={'operator'}
298306
/>
299-
<CheckMkAsyncSelect
307+
<CheckMkGenericAsyncSelect<string | undefined>
300308
onChange={(val) => onChange({ ...value, tag: val ?? '' })}
301309
value={value.tag}
302310
inputId={'tag'}
@@ -306,7 +314,7 @@ const SingleTag = (props: {
306314
);
307315
};
308316

309-
interface HostTagFilterProps extends CommonProps<'host_tags'> {
317+
interface HostTagFilterProps extends CommonProps<RequestSpec['host_tags']> {
310318
autocompleter: (
311319
prefix: string,
312320
mode: 'groups' | 'choices',
@@ -336,7 +344,7 @@ export const HostTagFilter: React.FC<HostTagFilterProps> = (props) => {
336344
);
337345
};
338346

339-
interface HostLabelProps extends CommonProps<'host_labels'> {
347+
interface HostLabelProps extends CommonProps<RequestSpec['host_labels']> {
340348
autocompleter: (value: string) => Promise<Array<SelectableValue<string>>>;
341349
}
342350

@@ -376,10 +384,10 @@ export const HostLabelFilter: React.FC<HostLabelProps> = (props) => {
376384
);
377385
};
378386

379-
interface TopLevelComponentProps<Key extends keyof RequestSpec, Value = RequestSpec[Key]> {
387+
interface TopLevelComponentProps<Key extends keyof RequestSpec> {
380388
label: string;
381389
requestSpecKey: Key;
382-
onChange(value: Value): void;
390+
onChange(value: RequestSpec[Key]): void;
383391
}
384392

385393
type ChildComponent = React.ReactElement<TopLevelComponentProps<FilterEditorKeys>, JSXElementConstructor<unknown>>;

0 commit comments

Comments
 (0)