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

fix(DropdownMenu/ContextMenu): improve generic types #2490

Closed
wants to merge 7 commits into from

Conversation

yassilah
Copy link
Contributor

@yassilah yassilah commented Oct 29, 2024

πŸ”— Linked issue

Resolves #2140

❓ Type of change

Implements same type inference as #2482

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@yassilah yassilah marked this pull request as ready for review October 29, 2024 20:49
@benjamincanac benjamincanac changed the title fix(DropdownMenu,ContextMenu): improve generic types fix(DropdownMenu/ContextMenu): improve generic types Oct 29, 2024
@benjamincanac benjamincanac added the v3 #1289 label Oct 29, 2024
Copy link

pkg-pr-new bot commented Oct 29, 2024

pnpm add https://pkg.pr.new/@nuxt/ui@2490

commit: 4a04950

@benjamincanac
Copy link
Member

I just tried it but it doesn't seem the types are well resolved πŸ€”
CleanShot 2024-10-29 at 22 30 57@2x

@yassilah
Copy link
Contributor Author

I think this is linked to what I had suggested for the CommandPalette here #2260 (comment)

If your items don't all have the icon property then it is expected for TS to warn you as you'd be trying to access a potentially non-existing property. It's possible to go around that in two different ways:

  • wrap it inside a v-if="'icon' in item" and then it'd work and infer types correctly;
  • turn the union into an intersection and then it'd work even without v-if

Should I go for the latter?

@benjamincanac
Copy link
Member

Yes I think that would be the most convenient 😊

@benjamincanac
Copy link
Member

@yassilah Trying to understand your last changes, why do we need to use this new intersection type for DropdownMenu and ContextMenu but don't need it in NavigationMenu? πŸ€”

My thought would be that the types are wrong in DropdownMenuContent and ContextMenuContent.

@yassilah
Copy link
Contributor Author

So I decided to go for this "optional intersection" instead of just "intersection" for 2 reasons:

  • i thought it kind of defeats the purpose of type-safety to say that items all have the same properties when we know they don't;
  • there's also a scenario where two items have conflicting types for the same property such as [{ foo: 'foo' }, { foo: 2 }] which would, in the previous implementation result into having { foo: never };

So I think this new way of inferring the types is safer and handles conflicting types properly.

@benjamincanac
Copy link
Member

Do types work for you in this PR? I no longer have autocomplete on slots nor slot props πŸ€”

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v3 #1289
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants