Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
amritk opened this issue Feb 11, 2025 · 2 comments
Closed

Comments

@amritk
Copy link

amritk commented Feb 11, 2025

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

@amritk
Copy link
Author

amritk commented Feb 11, 2025

Related issue, though not exactly the same #6532

@HotCherryPie
Copy link

Hello! This is expected behavior, because this is exactly what this option is for)

type Props = { hello?: string }

const foo: Props = { hello: undefined };

Without exactOptionalProperties: true — no error

With exactOptionalProperties: true — error

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)

Since props passed to component as object, Your example is not really relevant to this issue.

@vuejs vuejs locked and limited conversation to collaborators Feb 13, 2025
@edison1105 edison1105 converted this issue into discussion #12869 Feb 13, 2025

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

3 participants