Skip to content

Commit

Permalink
chore: even better typing for multiple & selected/value props (thanks…
Browse files Browse the repository at this point in the history
  • Loading branch information
vnphanquang committed Oct 17, 2022
1 parent e068bbb commit d271923
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
6 changes: 4 additions & 2 deletions apps/svelte-kit/src/routes/select/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,17 @@
<Select {options} placeholder="Please select" selected={options[2]} />

<!-- this should cause ts error (selected must be array) -->
<!-- <Select {options} placeholder="Please select" multiple selected={options[2]} /> -->
<!-- <Select {options} placeholder="Please select" multiple selected={options[2]} />
<Select {options} placeholder="Please select" multiple={true} selected={options[2]} /> -->
</div>

<div>
<h5>Multiple</h5>
<Select {options} placeholder="Please select" multiple selected={options.slice(2, 5)} />

<!-- this should cause ts error (selected must be single value) -->
<!-- <Select {options} placeholder="Please select" selected={options.slice(2, 5)} /> -->
<!-- <Select {options} placeholder="Please select" selected={options.slice(2, 5)} />
<Select {options} placeholder="Please select" multiple={false} selected={options.slice(2, 5)} /> -->
</div>

</article>
Expand Down
3 changes: 2 additions & 1 deletion apps/svelte-kit/src/routes/select/Select.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
type Value = $$Generic;
type Option = $$Generic<BaseOption<Value>>;
type Multiple = $$Generic<true | false | undefined>;
type $$Props = SelectProps<Option>;
type $$Props = SelectProps<Option, Multiple>;
type SelectedOption = $$Props['selected'];
type SelectedValue = $$Props['value'];
Expand Down
26 changes: 9 additions & 17 deletions apps/svelte-kit/src/routes/select/Select.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ export interface GroupConfig<Option> {

export type GroupProp<Option> = boolean | Grouper<Option> | Partial<GroupConfig<Option>>;

export interface SelectBaseProps<O extends BaseOption<V>, V = O['value']> {
export interface SelectProps<
O extends BaseOption<V>,
M extends true | false | undefined,
V = O['value'],
> {
id?: string;
placeholder?: string;
open?: boolean;
Expand All @@ -48,20 +52,8 @@ export interface SelectBaseProps<O extends BaseOption<V>, V = O['value']> {
clearable?: boolean;
hideExpansionIndicator?: boolean;
options: O[];
multiple?: M;
selected?: undefined extends M ? O : true extends M ? O[] : O;
// selected?: M extends undefined ? O : M extends true ? O[] : O;
value?: undefined extends M ? V : true extends M ? V[] : V;
}

export interface SingleSelectProps<O extends BaseOption<V>, V = O['value']> extends SelectBaseProps<O, V> {
multiple?: false;
selected?: O;
value?: V;
}

export interface MultipleSelectProps<O extends BaseOption<V>, V = O['value']> extends SelectBaseProps<O, V> {
multiple: true;
selected?: O[];
value?: V[];
}

export type SelectProps<O extends BaseOption<V>, V = O['value']> =
| SingleSelectProps<O, V>
| MultipleSelectProps<O, V>;

0 comments on commit d271923

Please sign in to comment.