core icon indicating copy to clipboard operation
core copied to clipboard

Using unions in defineProps in conjunction with withDefaults breaks emits type inference

Open darkbasic opened this issue 1 year ago • 1 comments

Vue version

3.5.11

Link to minimal reproduction

https://play.vuejs.org/#eNqNVNtu00AQ/ZWRX5JIkaOq9MVyArQECSRKBbxhhIw9TrbsTbtrN1Hqf2d2NwlO1FZ989zOnJk5613yXuu0azHJktxWhmkHFl2rgZdyNS8SZ4tkUUgmtDIObpTQ0BglYJTOvOFLR4XMZ7GWMslwKDQvHZIFkIcaxxxHghPb8FUk4LbaO2Qr/qAhO+tK3nrPFRnvxHbZoXRkjnEC8wXsenLPCDGfDeCTKRGslGzYKr23StIUO9+0SCrqyjiar9oxJWmIDELEx0rO1cPn4HOmxenBX62x+vuE/95uvK9I7gxaNB3RP8ZcaVboYnj5/RY39H0MClW3ftgXgt/QKt56jjHtupU10R7kBbafwv6ZXP2wy41DaQ9DeaI+sw/5RUL38At/bvT/dC/TN6GukD1t8XDL18lgBw/MrT9gU7bc2SnU2DCJd0bpo7EUzFno92KJKqF66dA0ZYVwXVq8KU0diiK/oIwMrDM0p3cwuuzbgwMeQbacE91ToNsgIA8FtF+UtX0Sm9SWwSiqjagABLllhBn0d4760ZSV394rcZt9+hD5MEdA9nkQwOZDxo8njXwijWwd6NBhfrLm8WDLuU9fjCfTyMIvilhYJdCtqSnR6CenA32JDyrmy1KcE4x9ka5GbQcnzEPB/jlm8JPq9lC/qJIovPz4a9aFDwi/juCaRd/5O/7dofGqJgVeplfpxUXS/wMjZpcD

Steps to reproduce

The SFC Playground is a bit broken because even if you remove withDefaults it still won't be able to infer the type if you hover on the emit with your mouse. So I suggest to copy paste the code locally: vscode will be able to infer the event type once you remove withDefaults.

Alternatively you can try to assign the event to a function which expects a different type and run vue-tsc --build --force. Without withDefaults it will complain about the mismatch: image

If you leave withDefaults it won't complain because he thinks the inferred type is any: image

What is expected?

Using withDefaults in conjuction with union props shouldn't break emits type inference.

What is actually happening?

Using withDefaults in conjuction with union props breaks emits type inference.

This is without withDefaults: image

While this is with withDefaults: image

System Info

System:
    OS: Linux 6.10 Arch Linux
    CPU: (16) x64 AMD Ryzen 9 PRO 7940HS w/ Radeon 780M Graphics
    Memory: 26.39 GB / 58.64 GB
    Container: Yes
    Shell: 5.2.37 - /bin/bash
  Binaries:
    Node: 20.17.0 - /run/user/1000/fnm_multishells/57865_1728023771872/bin/node
    Yarn: 4.5.0 - /run/user/1000/fnm_multishells/57865_1728023771872/bin/yarn
    npm: 10.8.2 - /run/user/1000/fnm_multishells/57865_1728023771872/bin/npm
    Watchman: 20240414.112832.0 - /usr/bin/watchman
  Browsers:
    Chromium: 129.0.6668.89
  npmPackages:
    vue: ^3.5.11 => 3.5.11

Any additional comments?

No response

darkbasic avatar Oct 04 '24 15:10 darkbasic

~~Solution in https://github.com/vuejs/core/issues/12012#issuecomment-2377956415~~

KazariEX avatar Oct 05 '24 04:10 KazariEX

Workaround: https://github.com/vuejs/core/issues/12012#issuecomment-2452963489

KazariEX avatar Nov 02 '24 11:11 KazariEX