Skip to content

useVModels mistakenly marks returned ref as possibly undefined if the property is optional #3546

@IlyaSemenov

Description

@IlyaSemenov

Describe the bug

useVModels mistakenly marks the returned ref object as possibly undefined if the respective property is optional.

This is not correct. The ref is always returned. The ref value is possibly undefined, but not the ref object itself.

Example:

const props = defineProps<{
  foo: string;
  bar?: string;
}>();

const emit = defineEmits<{
  'update:foo': [foo: string];
  'update:bar': [bar: string | undefined];
}>();

const { foo, bar } = useVModels(props, emit);
const bar2 = useVModel(props, 'bar', emit);

foo.value = 'foo'; // OK
bar.value = 'bar'; // 'bar' is possibly 'undefined' <------ Type error (but no runtime error)
bar2.value = 'bar2'; // OK

How to reproduce: in StackBlitz, run npm test.

Reproduction

https://stackblitz.com/edit/nuxt-starter-vj3utk?file=components%2Ftest.vue

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.4.2 - /usr/local/bin/npm
    pnpm: 8.9.2 - /usr/local/bin/pnpm
  npmPackages:
    @vueuse/core: ^10.6.1 => 10.6.1

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions