Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support type generics for component typings #1395

Closed
AlexandreBonaventure opened this issue Jun 17, 2020 · 1 comment
Closed

Support type generics for component typings #1395

AlexandreBonaventure opened this issue Jun 17, 2020 · 1 comment

Comments

@AlexandreBonaventure
Copy link
Contributor

What problem does this feature solve?

I don't know if there is already a way to handle type generics for component typings but if not it could be a nice addition. The benefit would be improved typings overall when it comes to generic components, especially when you use emits options and later with the use type-aware template devtool (in IDEs)

Use-case:
Let's say I have a component to list items:
Right now I can do this

<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
  props: {
    items: {
      type: Array as () => unknown[],
      required: true,
    },
  },
  emits: {
    click: (item: unknown) => {
      // type is lost :(
    },
  },
  setup (props, { emit }) {
    
  },
}));
</script>

<template>
<ul>
    <li v-for="item in items" @click="emit('clickItem', item)">{{ item.name }}</li>
</ul>
</template>

What does the proposed API look like?

With generics support it could be like that :

<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent(<T extends []>() => ({
  props: {
    items: {
      type: Array as () => T,
      required: true,
    },
  },
  emits: {
    click: (item: T) => {
      // perform runtime validation
    },
  },
  setup (props, { emit }) {
    const set = computed(() => new Set<T>(props.items))
  },
}));
</script>

<template>
<ul>
    <li v-for="item in items" @click="emit('clickItem', item)">{{ item.name }}</li>
</ul>
</template>
@HcySunYang
Copy link
Member

Close this one because it is inactive and is a duplicate of #3102, we can track this in #3102 because it provides a repro and is more active.

@github-actions github-actions bot locked and limited conversation to collaborators Oct 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants