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

feat(kit,schema): improve type safety of defineNuxtModule options #26850

Merged
merged 10 commits into from
Jun 10, 2024

Conversation

DamianGlowala
Copy link
Member

@DamianGlowala DamianGlowala commented Apr 19, 2024

🔗 Linked issue

📚 Description

Currently, defining the defaults for options in defineNuxtModule does not impact the type of resolvedOptions param within the setup function. Their types are disconnected, which this PR aims to address.

As of today, TypeScript does not support partial inference of generic type params. Due to this fact, I'd like to suggest adding an extra function call (named with), which can handle merging inferred defaults with the module options generic type param and therefore improve both the type safety and DX:

type ModuleOptions = {
  option1: string
  option2: number
}

defineNuxtModule<ModuleOptions>().with({
  defaults: {
    option1: 'foo'
  },
  setup: (resolvedOptions) => {}
         // ^? { option1: string, option2?: number | undefined }
})

I put effort to add it in a non-breaking way. It would be good to double check nothing changed in the current implementation.

Copy link

stackblitz bot commented Apr 19, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@DamianGlowala DamianGlowala changed the title feat(kit,schema): improve type inference in defineNuxtModule feat(kit,schema): improve type safety in defineNuxtModule Apr 19, 2024
@DamianGlowala DamianGlowala changed the title feat(kit,schema): improve type safety in defineNuxtModule feat(kit,schema): improve type safety of defineNuxtModule options Apr 22, 2024
@DamianGlowala DamianGlowala self-assigned this Apr 23, 2024
Copy link
Member

@danielroe danielroe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!

I think we should add some docs to the module author guide. (Can be a subsequent PR.)

@danielroe danielroe merged commit c06a683 into main Jun 10, 2024
60 checks passed
@danielroe danielroe deleted the feat/define-nuxt-module-overhaul branch June 10, 2024 10:16
@github-actions github-actions bot mentioned this pull request Jun 10, 2024
4 tasks
danielroe added a commit that referenced this pull request Jun 10, 2024
@DamianGlowala DamianGlowala restored the feat/define-nuxt-module-overhaul branch June 11, 2024 07:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants