Description
Vue version
3.5.17
Link to minimal reproduction
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 a prop whose type is possibly
undefined
, and then set it's default value to beundefined
Example:
const props = withDefaults(
defineProps<{
stringProp: string | undefined
}>(),
{
stringProp: undefined,
}
)
- Notice the error:
Argument of type '{ stringProp: undefined; }' is not assignable to parameter of type 'InferDefaults<LooseRequired<__VLS_Props>>' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
Types of property 'stringProp' are incompatible.ts(2379)
...even though 'undefined' has been added to the types of the target's properties.
What is expected?
When a prop's type is possibly undefined
, it should be possible to set it's default value as undefined
. This functionality is correct when the TSConfig options exactOptionalPropertyTypes
and strictNullChecks
are not true
What is actually happening?
You cannot set a prop's default value to undefined
even if it's listed as a possible type. The error even suggests adding undefined
to the type definition even though it's already there
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.17
Any additional comments?
One reason why we'd want to set a default to be undefined
is to satisfy this Vue ESLint rule to require optional props have a default set.
It appears this issue comes from the InferDefault
type:
core/packages/runtime-core/src/apiSetupHelpers.ts
Lines 322 to 326 in a0bd1f5
Any "Non-Native" types are filtered out and instead required to be defined as a function (to account for objects, arrays, and other mutable reference types, see the first info block under this section in the docs).
For some reason, this also filters out undefined
, even though it is a primitive JavaScript value?
We testing added undefined
to the NativeType
type locally and this resolved our issue, though we're not sure if this is the right approach and would like feedback before opening a PR.