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

overriding Route type when initializing UniversalRouter #185

Open
1 of 3 tasks
webican opened this issue Jan 30, 2020 · 5 comments
Open
1 of 3 tasks

overriding Route type when initializing UniversalRouter #185

webican opened this issue Jan 30, 2020 · 5 comments

Comments

@webican
Copy link

webican commented Jan 30, 2020

I'm submitting a ...

  • bug report
  • feature request
  • other (Please do not submit support requests here (below))

I'm having trouble overriding Route type when initializing UniversalRouter:

interface MyRouteType {
  customProperty: any;
}

const router = new UniversalRouter<MyContextType, MyRouteType>(routes, {
  async resolveRoute(context, params) {
    if (context.route.customProperty) {
       console.log(context.route.customProperty);
    }
  },
});

My IDE is complaining that customProperty doesn't exist on context.route

I fixed it like this: http://www.mergely.com/oC2xyDZh/, but I'm still new to typescript and I'm not sure if my fix is any good.

@frenzzy
Copy link
Member

frenzzy commented Jan 30, 2020

@jazblueful, hi!
Currently the router does not accept a route type. The second type variable is route result but not route.

- new UniversalRouter<Context, Route>(routes, options)
+ new UniversalRouter<Context, Result>(routes, options)

so, the only way to extend Route type is to add an extended interface in your code and typescript will merge them.

import { Route } from 'universal-router'

interface Route {
  customProperty: any;
}

// ...

I want to allow to redefine Route type in the next router version, see todo in #183

@webican
Copy link
Author

webican commented Jan 30, 2020

hi @frenzzy,
Thanks for a quick reply and clarification. It makes sense regarding the Result type.

However when I try to extend Route type this way I get: "TS2440: Import declaration conflicts with local declaration of 'Route'."

@webican
Copy link
Author

webican commented Jan 30, 2020

It looks like merging imported and local interfaces was removed in TS 3.7 https://devblogs.microsoft.com/typescript/announcing-typescript-3-7-rc/#local-and-imported-type-declarations-now-conflict and only option for me is to wait for the next router version.

@frenzzy
Copy link
Member

frenzzy commented Jan 30, 2020

Have you tried to add a universal-router.d.ts in your project source root with the following content?

declare module 'universal-router' {
  export interface Route {
    customProperty: any;
  }
}

Just found an article: TypeScript: Adding Custom Type Definitions for Existing Libraries

Also I will try to find time next week to update PR #183 and release v9, but it is not guaranteed 🤷‍♂️

@webican
Copy link
Author

webican commented Jan 30, 2020

Awesome! thank you @frenzzy :)

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

No branches or pull requests

2 participants