Is there a way to define whether a property is required or not based on generic? #13505
Answered
by
LinusBorg
PindaPixel
asked this question in
Help/Questions
-
For example for a dropdown list I'd want something like the following: <script setup lang="ts" generic="T">
defineProps<{
items: T[];
keyField: T extends object ? keyof T : never;
}>();
</script> |
Beta Was this translation helpful? Give feedback.
Answered by
LinusBorg
Jun 20, 2025
Replies: 2 comments 1 reply
-
I highly doubt it considering that as the types are used to generate runtime code, so it has to be either always required or always optional, as you can't have both at runtime. |
Beta Was this translation helpful? Give feedback.
0 replies
-
You can achieve something like this with a discriminated union, but it's a bit verbose and the type error is not very helpful: |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
PindaPixel
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can achieve something like this with a discriminated union, but it's a bit verbose and the type error is not very helpful:
https://play.vuejs.org/#eNp9kk1v2zAMhv8Kp4tbwIvRdCfPzbANHbYdtqJLsUOdg2PTnhpZEiQ5zeD6v5eSmzTp18UWXz6iXkrs2WetJ+sOWcoyWxquHVh0nZ7lkrdaGQc9GKxhgNqoFiJCo13qq2r1gz5JfOArUbpU0jpobQNnfvNR9B2FUPBXGVG9i463AHfY2hNirk/i6eJAnXq1hyKFqIiGuPeLZTQs4IA69RQB8TSGuhAWqUiWjG1QAxQQpUXhkCKALPhNw9aznI3H5wxW+P99zVFUJBYUJ0Rnyd5WFjNn6dyaN5MbqyRdVu8L5qykilyg+a0dJ185SyFkfK6gnm9/Bs2ZDuOtXv7DcvWCfmM3XsvZhUGLZo052+VcYRp0Y/r8zy/c0HqXbFXVCaLfSF6iVaLzHkfsSycrsr3HBbc/wrty2czt+cahtNumvFFPDoHPGb2zv8zXWn+0ezr5EPblcqBb3M7I02kDUciGbt9RGWhQouElhXOgXlBWFi6xVKbKrDNkLoZOrqS6lbMYrnaI7NolGriDEaLFUimBBXVMT1hhzS…