feat(compiler-sfc): enable reactive props destructure by default #7986
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Reactive props destructure was part of the Reactivity Transform proposal. The
$ref
part of Reactivity Transform has been dropped, but we are landing reactive props destructure as a stable feature and enabling it by default.withDefaults()
is deprecated in favor of using the destructure default value syntax:withDefaults()
was marked as deprecated in this PR but the deprecation has been reverted in 4af5d1b.<script>
block will be consistent with usage in<template>
.let
.watch()
directly on a destructured prop is expected to be a common gotcha, so this is detected and will result a compile-time error that nudges the user to use a getter instead.Caveat
You cannot enforce the type of the destructure default value in TypeScript, so technically this is possible:
The resulting type of
foo
will benumber | 'hello'
, i.e. the user could widen the type of the prop by mistake. That said, in most cases, this should be caught by code that expects the original type of the prop.To address this, we perform compile-time check to catch the most common cases of type mismatch (cf9ebaf). This check is limited to cases where both the default value type and the declared type can be safely inferred. It's not comprehensive, but should provide a good enough safeguard for the most common cases.