Skip to content

Broken types when using render functions with generic components & defineModel #12307

@DrWarpMan

Description

@DrWarpMan

Vue version

3.5.12

Link to minimal reproduction

https://github.com/DrWarpMan/vue-generic-bug

Steps to reproduce

The bug is described in the comments of the code.

App.vue

<script setup lang="ts">
import { h, ref } from 'vue';
import Comp from './Comp.vue';

const foo = ref<string>('foo');

// val should be of type 'string', not 'unknown'
const FunctionalComponent = () => h(Comp, {
  /* unknown */ modelValue: foo.value, 
  ['onUpdate:modelValue']: (val /* unknown */) => foo.value = val,
});
</script>

<template />

Comp.vue

<script setup lang="ts" generic="T">
defineModel<T>({required: true});
</script>

<template />

What is expected?

Render functions should have proper type based on the provided value to the generic component.

What is actually happening?

Render functions have 'unknown' type, regardless of the provided value to the generic component.

System Info

No response

Any additional comments?

Issue was moved from language-tools repo.

Activity

edison1105

edison1105 commented on Nov 1, 2024

@edison1105
Member

should be

const FunctionalComponent = () => h(Comp<string>, {
  modelValue: foo.value, 
  ['onUpdate:modelValue']: (val) => foo.value = val,
});
DrWarpMan

DrWarpMan commented on Nov 1, 2024

@DrWarpMan
Author

So you have to manually specify it, it can't get inferred?

edison1105

edison1105 commented on Nov 1, 2024

@edison1105
Member

For generic components, you need to specify the type; there is no way to infer it automatically.

DrWarpMan

DrWarpMan commented on Nov 1, 2024

@DrWarpMan
Author

Okay, now that I see it, it seems pretty straightforward, but I wonder if this should be mentioned somewhere in the docs?

In any case, the issue can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @edison1105@DrWarpMan

        Issue actions

          Broken types when using render functions with generic components & defineModel · Issue #12307 · vuejs/core