Open webican opened 4 years ago
@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
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'."
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.
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 🤷♂️
Awesome! thank you @frenzzy :)
I'm submitting a ...
I'm having trouble overriding Route type when initializing UniversalRouter:
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.