Subscribe on changes!

Boolean props shorthand does not work on components typescript defineprops

avatar
Mar 3rd 2022

Version

3.2.31

Reproduction link

sfc.vuejs.org/

Steps to reproduce

see console.log tile is '' not true

What is expected?

true

What is actually happening?

''

avatar
Mar 3rd 2022

That's because you define the prop in a way the compiler can not understand.

See docs: https://vuejs.org/api/sfc-script-setup.html#type-only-props-emit-declarations

As of now, the type declaration argument must be one of the following to ensure correct static analysis:

  • A type literal
  • A reference to an interface or a type literal in the same file

yours is neither - it's reading a property from an interface, and the compiler is not yet smart/complex enough to evaluate that.

avatar
Mar 4th 2022

@LinusBorg is there any method for me to link it to a outside interface? because I need interface to constrain other code ,and I want there are only one interface

avatar
Mar 4th 2022

Only the "normal" way to define props: with an actual object:

const props = defineProps({
  tile: {
    type: Boolean as PropType<BadgeProps['tile']>; // the casting is unnecessary here as `tile` is already of a primitive type.
  }
})
avatar
Mar 4th 2022

@LinusBorg Thanks!