Optional prop with default may incorrectly be undefined in template when exactOptionalPropertyTypes
and strictNullChecks
are true
#5338
Labels
good reproduction ✨
This issue provides a good reproduction, we will be able to investigate it first
🔨 p3-minor-bug
Vue - Official extension or vue-tsc version
2.2.8 and 3.0.0-alpha.4
VSCode version
1.99.3
Vue version
3.5.13
TypeScript version
5.8.3
System Info
package.json dependencies
Steps to reproduce
tsconfig.json
, undercompilerOptions
, enableexactOptionalPropertyTypes
andstrictNullChecks
"strict": true
will also enablestrictNullChecks
and cause the issueExample:
...even though it is impossible for the variable to ever be undefined.
props.stringWithDefault
instead ofstringWithDefault
and notice the error goes awaystringWithDefault
is (incorrectly)string | undefined
whileprops.stringWithDefault
is (correctly)string
What is expected?
When passing the prop in the template, it should never be
undefined
since there is a default value set. The expected behavior is seen when prepending the variable withprops.
to use the variable through the props objectWhat is actually happening?
The variable is possibly
undefined
even though there is a default value, causing a typescript error when passing it to a prop that is requiredLink to minimal reproduction
https://play.vuejs.org/#eNqFU8Fu2zAM/RVOpxZobAzbyXAzbGkP26Er1mC76GLYdOxWljSJblIY/vdRcltnadMiF4nv+eXxiRzEV2uT+x5FJnJfutYSeKTegir05lwK8lIspW47axzBynQWamc6kCJJwy18K4XUUpdGewLrjPVwDtuWmgusi16RP2GYKqxbjdcBzodQkOTJtXrzZ2Z+yWCqBXxcnpyehcMxdsYmWDUcfxcq2Ij0UepTqfN06oa984Wws6og5BtA3nxcDsPjP+3pwTjmKWOR82GxgAuDHrQh2Bp3B4vFhMQMMod/+9ZhdRNVOKgXclKks9S6QaiNUmbLpKjn3xOMSSZHZPN0ryVxxs/E8dftJrn1RvNbcmbA8ZQs3Sp0Py21/DxSZBCRgBXBzY9YI9cjZzfVywbLu1fqt34XalJcO/To7qe8J4wKt0G2FuDLmyvc8fkZ7EzVK2a/Af5Cb1QfPE60b72u2PYeL7r9HqeQ81j7yx2h9k9NPRqNPx4VKXBXlDR1XagwdOjoYf1g8WVfIeGSrnqlVqHzZ0LAx8iSgoc8vNKxKOf2PyWf43dSj/wqTwvyznIdrAbA/7MwL0VYiYPRfm24V02rqrirRqOedpLdDge687zvTZMY/wHSU2oA
Any additional comments?
I originally submitted this issue to
vuejs/core
, but after further investigation I believe it belongs in this repo. vuejs/core#13236I don't think this belongs in the core repo because the generated build is completely valid, the error only happens during type checking
The minimum reproduction can demonstrate the issue in the browser, but I was also able to replicate it locally, and can provide a full repository if needed
In the reproduction link, the only changes made to the
tsconfig.json
file is settingexactOptionalPropertyTypes
andstrictNullChecks
are true. If either are not present, the issue doesn't happen.I'm not sure if it's relevant, but if you turn the reproduction back to use typescript version
5.6.3
the issue goes away because all the props are of typeany
. The issue appears in the following5.7.0-beta
version because the type information suddenly appears. I didn't notice anything relevant in a quick skim of the 5.7 release notes.Related: #3728
Sort of related to vuejs/core#6532 , but different because that issue is about still allowing
undefined
as a value, where this issue is thatundefined
is a possible when when it should be impossibleThe text was updated successfully, but these errors were encountered: