fix(ComboboxRoot): fix ignored type error #1713
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request removes the
@ts-expect-error
from here:reka-ui/packages/core/src/Combobox/ComboboxRoot.vue
Lines 95 to 100 in c9f4a2a
And converts it to the following:
reka-ui/packages/core/src/Combobox/ComboboxRoot.vue
Lines 95 to 99 in 69da1b1
I hope this is acceptable.
Reasoning
In order to understand this change, we need to have a look at how
useVModel
infers the type of thedefaultValue
option.The
useVModel
function takes 3 generic parameters, of which the first two are important in this case, since they are used for the type of thedefaultValue
option:P extends object
K extends keyof P
Name extends string
(unimportant in this case, but included for completion)useVModel
usesP
andK
in order to set the first generic parameter ofUseVModelOptions
toP[K]
. And the first generic parameter ofUseVModelOptions
is used to set the type of thedefaultValue
option.The generic parameter
P
will be infered totypeof props
, since we pass it theprops
object. And theK
parameter will be infered to'modelValue'
, since we pass it the'modelValue'
string. And we know that thedefaultValue
option has the typeP[K]
in the generic case, so in this specific case it would have the type(typeof props)['modelValue']
.Now when setting the

defaultValue
option (type(typeof props)['modelValue']
) toprops.defaultValue
(typeT | T[] | undefined
fromComboboxRootProps['defaultValue']
), we get the following type mismatch error:(Note: I used a temporary variable here with the same type as
defaultValue
option, in order to make the error message more clear)So it seems, that the type of
(typeof props)['modelValue']
is notT | T[] | undefined
like we would expect, but rather(T | T[] | undefined) & boolean
. The& boolean
part is added here bydefineProps
, but I do not know why. Maybe it is there due to an edge case for the boolean casting vue feature? I am not sure.In any case, the
(T | T[] | undefined) & boolean
type is wrong and it should really beT | T[] | undefined
, which is achieved by explicitly castingprops
toComboboxRootProps
. ThenP
would be infered toComboboxRootProps
andP[K]
toComboboxRootProps['modelValue']
, which is exactlyT | T[] | undefined
.There may be more solutions to this problem, but I chose the
props as ComboboxRootProps<T>
solution, because theprops
variable is really a type ofComboboxRootProps<T>
, with a little type acrobatics added by vue'sdefineProps
. But since vue's type is wrong in this case, I cast it back toComboboxRootProps<T>
just for theuseVModel
call.