Description
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
System:
OS: Linux 6.13 Ubuntu 24.04.2 LTS 24.04.2 LTS (Noble Numbat)
CPU: (8) x64 Intel(R) Core(TM) i7-8665U CPU @ 1.90GHz
Memory: 2.05 GB / 15.27 GB
Container: Yes
Shell: 5.2.21 - /usr/bin/bash
Binaries:
Node: 22.14.0 - ~/.nvm/versions/node/v22.14.0/bin/node
Yarn: 1.22.22 - ~/.nvm/versions/node/v22.14.0/bin/yarn
npm: 10.9.2 - ~/.nvm/versions/node/v22.14.0/bin/npm
package.json dependencies
{
"dependencies": {
"vue": "^3.5.13"
},
"devDependencies": {
"@vitejs/plugin-vue": "^5.2.3",
"@vue/compiler-sfc": "^3.5.13",
"typescript": "^5.8.3",
"vite": "^5.4.18",
"vue-tsc": "3.0.0-alpha.4" // Error also occurs with 2.2.8
}
}
Steps to reproduce
- In your
tsconfig.json
, undercompilerOptions
, enableexactOptionalPropertyTypes
andstrictNullChecks
- Note: Setting
"strict": true
will also enablestrictNullChecks
and cause the issue
- Note: Setting
- In a component, create an optional prop that has a default value
Example:
const props = withDefaults(
defineProps<{
stringWithDefault?: string
}>(),
{
stringWithDefault: "defaultValue",
}
)
- Pass that variable to another component as a prop that is required (line 17 in the reproduction)
- Notice the error:
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.ts(2322)
...even though it is impossible for the variable to ever be undefined.
- To confirm, pass the variable as
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 with props.
to use the variable through the props object
What 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 required
Link to minimal reproduction
Any additional comments?
I originally submitted this issue to vuejs/core
, but after further investigation I believe it belongs in this repo. vuejs/core#13236
I 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 setting exactOptionalPropertyTypes
and strictNullChecks
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 type any
. The issue appears in the following 5.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 that undefined
is a possible when when it should be impossible