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(nuxt,schema): prefer unknown rather than any for signatures #21700

Merged
merged 3 commits into from Jun 22, 2023

Conversation

danielroe
Copy link
Member

@danielroe danielroe commented Jun 22, 2023

πŸ”— Linked issue

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ 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

This PR improves type safety within Nuxt in a number of locations where unspecified types fell back to any:

  • RuntimeConfig
  • PageMeta
  • NuxtApp['payload'] (accessible now from NuxtPayload interface)
  • ModuleMeta

It also fixes an issue with how we extended NitroRuntimeConfig.

The reason this is important is that type safety is purely cosmetic when accessing an unknown key. For example, if you rename a key in your runtime configuration, you aren't warned when accessing the old name of that key. If you do not like this behaviour, you can set typescript.strict to false in your nuxt.config - but it is highly recommended to keep strict mode enabled.

πŸ‘‰ Migration

It should not require any updating for end users in the case of RuntimeConfig as we already generate the types for every key we know about (and do not allow arbitrarily extending runtime configuration at runtime).

You can declare the types that are expected like this:

// this can be located in a declaration file (~/some-file.d.ts) or directly in your code base,
// ideally near where you use the augmented type
declare module '#app' {
  interface NuxtPayload {
    yourConfigKey?: number[]
  }
  interface PageMeta {
    myKey?: string
  }
}

// This should not be necessary but it is available should you need it as an escape hatch
declare module 'nuxt/schema' {
  interface RuntimeConfig {
    apiSecret: string
  }
}

// whenever augmenting types it is important that you either have an import or an export
// statement somewhere in the file, or your augmentations will 'overwrite' the original interface
export {}

πŸ“ Checklist

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

@nuxt-studio
Copy link

nuxt-studio bot commented Jun 22, 2023

βœ… Live Preview ready!

Name Edit Preview Latest Commit
Nuxt Docs Edit on Studio β†—οΈŽ View Live Preview e0a14ee

@danielroe danielroe merged commit dd5dff3 into main Jun 22, 2023
29 checks passed
@danielroe danielroe deleted the fix/unknown-signatures branch June 22, 2023 13:14
@danielroe
Copy link
Member Author

cc: @pi0

@github-actions github-actions bot mentioned this pull request Jun 22, 2023
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