Subscribe on changes!

defineProps Props interface containing reference to imported type not allowed when using: importedType | someOtherType

avatar
Jun 28th 2022

Vue version

3.2.37

Link to minimal reproduction

https://sfc.vuejs.org/#eNpNUctuwjAQ/JWtLwEJEnGNDFXVquqxt158CckGjOKHbAcqpf73rh1Kc7G8j9mZ2Z3Yi7XldURWM+5bJ20Aj2G0B6EBpLLGBXg1ykLvjIKirFKQAIXQ9/IEDnuI9465JHRrtA+g/An2qb4qPnAYDHwZN3RPxVpoXs18xERBQGWHJmDm5efdYZoyOEZeUZSzUtsxwHWrTIfDXjCqC3bY5VrWWFOG8jvK8iyUcLx6jGYbNkveqsaWF280uZ4SXNwLXrAaciblyEqKBTuHYH1dVb5vk/WLL407VfQr3aiDVFiiV9ujMzePjgYLlkZEoSNR/i0sbXjhUgTeyWs2BjBNpDwSgJxUc3qhOy9oeRwYGp2MJr3LQ70b87gT/ekOZENLHdD1TYvw6Yz1sz/ie67BByf1CX4SktTO/fPlbO7dw02G8xv2zTgEv+qwlxrzGJ7fw2q9eQysoaCXWOPyvLQCml52ZfC0gn8xSWxGHhtXgx7VEV3WgN/ZDHEl0tTH4i+pneju

Steps to reproduce

Create an interface in a different file Create a component with a defineProps using a interface, the interface containing a reference to the above interface type, then also make that property possible to be another type. Such as:

import Foo from './Foo' // Some type
interface Props { soup: Foo | string }
defineProps(...)

What is expected?

According to https://vuejs.org/guide/typescript/composition-api.html#syntax-limitations , The interface or object literal type can contain references to types imported from other files, which makes it seem that this is possible. It normally is possible with no errors when you solely use { soup: Foo }, However, when you use a | modifier, it breaks down.

What is actually happening?

When combining the type with an | , it will always Invalid prop: type check failed for prop "soup". Expected Null | String, got Number with value 1. , assuming it's anything other than the "string" type. In the above, you can tell that null was never allowed to be an acceptable value, which was odd.

System Info

System:
    OS: Windows 10 10.0.22000
    CPU: (12) x64 AMD Ryzen 5 3600X 6-Core Processor
    Memory: 6.63 GB / 15.93 GB
  Binaries:
    Node: 16.14.2 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.15 - C:\Program Files\nodejs\yarn.CMD
    npm: 8.11.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22000.120.0), Chromium (98.0.1108.50)
    Internet Explorer: 11.0.22000.120
  npmPackages:
    vue: 3.x.x => 3.2.37

Any additional comments?

Like I said, it is supposed to be allowed according to The interface or object literal type can contain references to types imported from other files. However, it breaks down if you use type | (any other type)