-
-
Notifications
You must be signed in to change notification settings - Fork 9k
Description
Vue version
v3.5.13
Link to minimal reproduction
Steps to reproduce
- In your
tsconfig.json, undercompilerOptions, enableexactOptionalPropertyTypesandstrictNullChecks- Note: Setting
"strict": truewill also enablestrictNullChecksand 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.stringWithDefaultinstead ofstringWithDefaultand notice the error goes awaystringWithDefaultis (incorrectly)string | undefinedwhileprops.stringWithDefaultis (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
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: 4.61 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.21 - ~/.nvm/versions/node/v22.14.0/bin/yarn
npm: 10.9.2 - ~/.nvm/versions/node/v22.14.0/bin/npm
npmPackages:
vue: ^3.0.0 => 3.5.13Any additional comments?
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: vuejs/language-tools#3728
Sort of related to #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