Subscribe on changes!

Boolean Casting of shorthand prop not working if the prop's type is generic

avatar
Oct 27th 2022

Vue version

v3.2.41

Link to minimal reproduction

https://sfc.vuejs.org/#eNqVUj1v3DAM/SuElmuBnIWuhuOg7dC1Q9AOcQbHxzsrkCWBopMAF//3ULId5AO5IINtiX58fHzkUf0MobgbUZWqih2ZwBCRxwC2dYfzRnFsVN04ADMETwy/vXwdOv5vuP+DDsl0f8mHCHvyA2wK/SEildk0rtJzHWGVC+MQbMuYa1T9j3omu5dciL1U7Fu3gxvvLbaSKoAM/FjGMFo2wSLgQ7CmM7xdkqEc/A7tv9aOKI1dbXq01m+uGwU6k67PGxVBjkhfkVCuGpJ/NKJUeFU6F17KVvrZAXWmZpO3QxuK2+idDOWYVS0/ZBYl5EiKiZ3pLnzMIZZax32XPL6NhaeDllNBo2MzYIFx2N6Qv49IQtyosxccWoJ3SFtCt0NCOsX5BvqON9FOjZuklZNr8Om2Nc44Rtq3HULOqi7hfJ1BPXuwulzCZbqu8/41gy7KFZ6xz/4LWqAsHURIw4ELiEzGHa6uoVyOj260VtpIG9p5FzkvQRQFO9wbh7Oi+b1qqr99zwt9YrsDYX08LlzTVOkUeL0C0xOhpUVg

Steps to reproduce

Props are displayed in the preview. Please refer to the File ComponentWithGenericProps.vue to see the definition

What is expected?

The shorthand prop is converted to boolean regardless of how it is defined

What is actually happening?

Given the following props definition, both the multiple and explicitBoolean properties are created on the props object. So far so good. However, if multiple is passed as short-hand flag, it's value will be an empty string rather than true.

interface Props<T = boolean> {
  multiple: T
  explicitBoolean?: boolean
  modelValue: T extends true ? string[] : string|null
}

const props = defineProps<Props<boolean>>()

System Info

No response

Any additional comments?

No response

avatar
Oct 27th 2022

I did find a work-around to get the boolean casting to work by using an additional union type with boolean (multiple: T | boolean). However, I have come to realise that TypeScript does not resolve the dependant property's type (modelValue is always a union type, even in a literal object with multiple set), so it really doesn't matter either way. Perhaps this use-case would work better if union types were supported on the top level for defineProps but that's probably asking too much.

avatar
Oct 27th 2022

I'll close this as it's explicitly unsupported right now. We did want to support genetics in the future though, and are in the process of investigating solutions