Skip to content

How to use generics in TSX #11374

Open
@TaurusWood

Description

@TaurusWood

Vue version

3.4.27

Link to minimal reproduction

https://play.vuejs.org/#eNp9U02PmzAQ/StTX2AlBFLbU5Yg9WOldg9ttY3UQ4i0CCYJu2BbtklTIf57x8aQ7DZNDsTz4Zk374179kHK+NAhW7BUl6qWBjSaTmY5r1splIEeFG5hgK0SLQSUGsyhz9gK748Ta8RGHylcCq4NtHoHS3s5DL5g0wj4JVRTvQlucp7zNBm7UR8yDLayKQySBZC6sgmd0+QswCJmNFXe1rv4SQtOiHubnrNStLJuUH2XpqbOOVuAi9hYQY1/3zufUR1Gk7/cY/l8wf+kj9aXsx8KNaoD5myOmULt0Izhu5/f8EjnOdiKqmso+0rwAbVoOotxTPvY8Ypgn+U5tF8duTXfrfTd0SDX01AWqM0cXH7OSItPV0Y/wX0Xv3f3cj4Qi5NQROAscYXbmrtqgiM30bnmrlHObmfZbdosuzW87JPwTr/l65pheAPLbII3ZjY1fcYdWY9+IDC8aHEBwUq0gZ/1hf++KJ9PgY1bJ3tStLeKw4s2xIP/99G0qg9uy+wvteDSqbA2ikiHIbObu8zZ3i5tzhzIZW+/8aFoOhzcbvoKyake0UsfBwePjihioOiakRAifqLqCvGv3tnEqGP8X0bda1kB7RrySsMDlkJV6ThHBAX/k2WhVEJq2go71DTjrZtpAav1hgB7uk66iAbjRuzCR5v1GIErEVuDhvs/0xcZ3r/N+vE+9R/ShOxZ0OkAZx3itpAh7UQGqcx6E1tp6JrMfGtP82Xu3Ur4sn7udUB9gwgCWzzY+GdwSSRLKxv+Ai3howE=

Steps to reproduce

In the official use case, when defining components using defineComponent, an example of using generics is as follows:

const Comp = defineComponent(
  <T extends string | number>(props: { msg: T; list: T[] }) => {
    // use Composition API here like in <script setup>
    const count = ref(0)

    return () => {
      // render function or JSX
      return <div>{count.value}</div>
    }
  },
  // manual runtime props declaration is currently still needed.
  {
    props: ['msg', 'list']
  }
)

I know that generic can be used to define in composite APIs, but child components and parent components are defined by defineComponent, so I want to know how to pass generic from the parent component in tsx###

What is expected?

like react, we can pass generics like this:

  return () => {
      
    return (
      <div>
        <Comp<{ name: string }> msg="hello" list={list.value} />
      </div>)
    }

What is actually happening?

I couldn't find any way to pass generics into child components,like this
WX20240717-095810

System Info

No response

Any additional comments?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    🍰 p2-nice-to-havePriority 2: this is not breaking anything but nice to have it addressed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions