Skip to content

vue-tsc failed to infer generic types of custom directives within templates #4682

@mon-jai

Description

@mon-jai

Vue - Official extension or vue-tsc version

Same as playground

Vue version

3.5.0-beta.1

TypeScript version

5.6.0-beta

System Info

No response

Steps to reproduce

<template>
  <!--
    vue-tsc failed to infer type of T
    Type '(newUser: User) => void' is not assignable to type '(newValue: { id: number; }) => void'.
      Types of parameters 'newUser' and 'newValue' are incompatible.ts(2322)
  -->
  <div v-example="[user, setUser]"></div>
</template>

<script lang="ts" setup>
import { ref } from "vue";

type User = {
  id: number;
  name: string;
};

const user = ref<User>({ id: 0, name: "User" });

function setUser(newUser: User) {
  user.value = newUser;
}

const vExample = {
  mounted<T extends { id: number }>(
    _elContainingTbody: HTMLElement,
    _binding: { value: [T, (newValue: T) => void] }
  ) { },
};

// T is inferred correctly by TypeScript
vExample.mounted(
  document.createElement("div"),
  { value: [user.value, setUser] }
)
</script>

What is expected?

vue-tsc should infer the type of T correctly in the template.

What is actually happening?

Type '(newUser: User) => void' is not assignable to type '(newValue: { id: number; }) => void'.
  Types of parameters 'newUser' and 'newValue' are incompatible.ts(2322)

Link to minimal reproduction

Playground link

Any additional comments?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions