Get component prop type
when using defineProps
with TypeScript in production
#11079
-
When using const props = defineProps<{
foo: number;
bar: string;
}>(); The
But in development, the type is available:
The reason is that the compiler basically skips adding type in production: core/packages/compiler-sfc/src/script/defineProps.ts Lines 262 to 269 in 32262a9 And then fallbacks to this code: core/packages/compiler-sfc/src/script/defineProps.ts Lines 295 to 296 in 32262a9 Is there a special reason for this? 😄 I see that there is already an exception for core/packages/compiler-sfc/src/script/defineProps.ts Lines 269 to 283 in 32262a9 I would be happy to make a PR with the other primitives added as well, but I am assuming this is done for a reason. ... and yes, we know that we could use the non-TS way of defining props, but we do really prefer the TS-way 😇 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This is by design. It's mentioned at https://vuejs.org/api/sfc-script-setup.html#type-only-props-emit-declarations. The main use of the There are a handful of cases where the |
Beta Was this translation helpful? Give feedback.
This is by design. It's mentioned at https://vuejs.org/api/sfc-script-setup.html#type-only-props-emit-declarations.
The main use of the
type
option is to validate prop values. That validation isn't performed in production, so thetype
is discarded to reduce the bundle size.There are a handful of cases where the
type
serves another purpose, not just validating prop values. For example, for aBoolean
, the value is interpreted differently to determine whether it istrue
orfalse
. In those cases thetype
needs to be retained, even in production.