Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Its possible define component with generic type? #2733

Copy link
Copy link
Closed
@martinsura

Description

@martinsura
Issue body actions

What problem does this feature solve?

Hello, first -> thanks for the Vue3. Its Awesome.

In my previous project I often use typescript generic. For example:

const CustomButton = defineComponent({
  props: {
    clickRequest:Function as PropType<(r?: Promise<any>) => void>,
    onClick: Function as PropType<(data:any) => void>
  },
  emits:["click"],
  setup(props, { emit }) {
    const internalClick = async () => {
      const data = await props.clickRequest()
      emit("click",data);
    }
    return () => {
      <button onClick={internalClick}>Action</button>
    }
  }
})

This example works as expected. Problem is any type around props definition.

What does the proposed API look like?

const CustomButton = defineComponent<T>({
  props: {
    clickRequest:Function as PropType<(r?: Promise<T>) => void>,
    onClick: Function as PropType<(data:T) => void>
  },
  emits:["click"],
  setup(props, { emit }) {
    const internalClick = async () => {
      const data = await props.clickRequest()
      emit("click",data as T);
    }
    return () => {
      <button onClick={internalClick}>Action</button>
    }
  }
})

Thanks

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.