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: allow returning Route from defineNuxtRouteMiddleware #878

Merged
merged 1 commit into from
Aug 14, 2023

Conversation

wattanx
Copy link
Collaborator

@wattanx wattanx commented Aug 9, 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)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Returning navigateTo in defineNuxtRouteMiddleware causes a type error.
In the case of a bridge, Route may be returned.

Reproduction

Run npx nuxi typecheck in the following environment
https://stackblitz.com/edit/github-3fgb91-hijxwf

middleware/redirect.ts:3:42 - error TS2345: Argument of type '() => Route | RawLocation | Promise<void | Route | NavigationFailure>' is not assignable to parameter of type 'RouteMiddleware'.
  Type 'Route | RawLocation | Promise<void | Route | NavigationFailure>' is not assignable to type 'RouteMiddlewareReturn | Promise<RouteMiddlewareReturn>'.
    Type 'Route' is not assignable to type 'RouteMiddlewareReturn | Promise<RouteMiddlewareReturn>'.
      Type 'Route' is not assignable to type 'Location'.
        Types of property 'name' are incompatible.
          Type 'string | null | undefined' is not assignable to type 'string | undefined'.
            Type 'null' is not assignable to type 'string | undefined'.

3 export default defineNuxtRouteMiddleware(() => {
                                           ~~~~~~~


Found 1 error in middleware/redirect.ts:3

πŸ“ Checklist

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

@@ -250,7 +250,7 @@ export const abortNavigation = (err?: string | Partial<NuxtError>) => {
throw err
}

type RouteMiddlewareReturn = void | Error | string | Location | boolean
type RouteMiddlewareReturn = void | Error | string | Location | boolean | Route
Copy link
Member

Choose a reason for hiding this comment

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

do we also need to accept undefined/null?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm, I investigated a bit and I don't think I have to accept it.

@danielroe danielroe changed the title fix: Fix type of defineNuxtRouteMiddleware fix: allow returning Route from defineNuxtRouteMiddleware Aug 9, 2023
@danielroe danielroe merged commit f09aafd into nuxt:main Aug 14, 2023
8 of 9 checks passed
@danielroe danielroe mentioned this pull request Oct 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants