Skip to content

Commit

Permalink
Fix type inference for select returns, return same type as input
Browse files Browse the repository at this point in the history
This means that if you use shorthand options of just strings, you'll get
strings back, which is what I would expect.
  • Loading branch information
jacobmischka committed Jun 29, 2022
1 parent 16350ae commit 680f185
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
44 changes: 23 additions & 21 deletions src/components/selectMultiple.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import { z } from 'zod'
import { T_IO_PROPS, T_IO_RETURNS, labelValue } from '../ioSchema'

type SelectMultipleProps = Omit<
T_IO_PROPS<'SELECT_MULTIPLE'>,
'options' | 'defaultValue'
> & {
options: (z.infer<typeof labelValue> | string)[]
defaultValue?: (z.infer<typeof labelValue> | string)[]
}
type SelectMultipleProps<Option extends z.infer<typeof labelValue> | string> =
Omit<T_IO_PROPS<'SELECT_MULTIPLE'>, 'options' | 'defaultValue'> & {
options: Option[]
defaultValue?: Option[]
}

export default function selectMultiple<Props extends SelectMultipleProps>(
props: Props
) {
const options = props.options.map(option => {
if (typeof option === 'string') {
return {
label: option,
value: option,
export default function selectMultiple<
Option extends z.infer<typeof labelValue> | string
>(props: SelectMultipleProps<Option>) {
const normalizedOptions: z.infer<typeof labelValue>[] = props.options.map(
option => {
if (typeof option === 'string') {
return {
label: option,
value: option,
}
} else {
return option as Exclude<Option, string>
}
} else {
return option
}
})
type Options = typeof options
const optionMap = new Map(options.map(o => [o.value, o]))
)
type Options = typeof props.options
const optionMap = new Map(
normalizedOptions.map((o, i) => [o.value, props.options[i]])
)

const defaultValue = props.defaultValue?.map(d => {
if (typeof d === 'string') {
Expand All @@ -42,7 +44,7 @@ export default function selectMultiple<Props extends SelectMultipleProps>(
props: {
...props,
defaultValue: defaultValue?.map(o => stripper.parse(o)),
options: options.map(o => stripper.parse(o)),
options: normalizedOptions.map(o => stripper.parse(o)),
},
getValue(response: T_IO_RETURNS<'SELECT_MULTIPLE'>): Options {
return response.map(r => optionMap.get(r.value)) as Options
Expand Down
31 changes: 16 additions & 15 deletions src/components/selectSingle.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import { z } from 'zod'
import { T_IO_PROPS, T_IO_RETURNS, richSelectOption } from '../ioSchema'

type SelectSingleProps = Omit<
T_IO_PROPS<'SELECT_SINGLE'>,
'options' | 'defaultValue'
> & {
options: (z.infer<typeof richSelectOption> | string)[]
defaultValue?: z.infer<typeof richSelectOption> | string
type SelectSingleProps<
Option extends z.infer<typeof richSelectOption> | string
> = Omit<T_IO_PROPS<'SELECT_SINGLE'>, 'options' | 'defaultValue'> & {
options: Option[]
defaultValue?: Option
}

export default function selectSingle<Props extends SelectSingleProps>(
props: Props
) {
const options = props.options.map(option => {
export default function selectSingle<
Option extends z.infer<typeof richSelectOption> | string
>(props: SelectSingleProps<Option>) {
const normalizedOptions = props.options.map(option => {
if (typeof option === 'string') {
return {
label: option,
value: option,
}
} else {
return option
return option as Exclude<Option, string>
}
})

type Options = typeof options
const optionMap = new Map(options.map(o => [o.value, o]))
type Options = typeof props.options
const optionMap = new Map(
normalizedOptions.map((o, i) => [o.value, props.options[i]])
)
const stripper = richSelectOption.strip()

const defaultValue =
Expand All @@ -33,13 +34,13 @@ export default function selectSingle<Props extends SelectSingleProps>(
label: props.defaultValue,
value: props.defaultValue,
}
: props.defaultValue
: (props.defaultValue as Exclude<Option, string>)

return {
props: {
...props,
defaultValue: defaultValue ? stripper.parse(defaultValue) : undefined,
options: options.map(o => stripper.parse(o)),
options: normalizedOptions.map(o => stripper.parse(o)),
},
getValue(response: T_IO_RETURNS<'SELECT_SINGLE'>) {
return optionMap.get(response.value) as Options[0]
Expand Down

0 comments on commit 680f185

Please sign in to comment.