Skip to content

Accept undefined as prop when exactOptionalPropertyTypes is enabled in tsconfig #12859

@amritk

Description

@amritk

Vue version

3.5.12

Link to minimal reproduction

https://github.com/amritk/vue-exact-optional-property-types

Steps to reproduce

Create a component like so

<script setup lang="ts">
const { hello = 'world' } = defineProps<{ hello?: string }>()
</script>
<template>
  <div>
    <p>{{ hello }}</p>
  </div>
</template>

enable exactOptionalProperties in tsconfig

What is expected?

You should be able to pass undefined into the prop as you can in typescript

const test = (hello: string = 'world') => {
	console.log(hello)
}

test()
test(undefined)

What is actually happening?

Image
It gives an error saying undefined is not assignable to string. However we have a default set so it will end up being a string.

src/App.vue:6:4 - error TS2379: Argument of type '{ msg: undefined; }' is not assignable to parameter of type '{ readonly msg?: string; } & VNodeProps & AllowedComponentProps & ComponentCustomProps & Record<string, unknown>' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
  Type '{ msg: undefined; }' is not assignable to type '{ readonly msg?: string; }' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
    Types of property 'msg' are incompatible.
      Type 'undefined' is not assignable to type 'string'.

   <HelloWorld :msg="undefined" />

System Info

System:
    OS: Linux 6.13 Arch Linux
    CPU: (20) x64 Intel(R) Core(TM) i9-10900KF CPU @ 3.70GHz
    Memory: 56.07 GB / 62.71 GB
    Container: Yes
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 22.14.0 - ~/.asdf/installs/nodejs/22.14.0/bin/node
    npm: 10.9.2 - ~/.asdf/installs/nodejs/22.14.0/bin/npm
    pnpm: 9.15.5 - ~/.asdf/installs/nodejs/22.14.0/bin/pnpm
    bun: 1.2.0 - ~/.bun/bin/bun
  npmPackages:
    vue: ^3.5.13 => 3.5.13

Any additional comments?

Is there a way to get this to work like it does in typescript without explicitly setting the type | undefined

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions