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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

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
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

1 participant