In my project, I am utilizing the NPM package called next-routes. The default export from this package is a class with a specific type definition:
export default class Routes implements Registry {
getRequestHandler(app: Server, custom?: HTTPHandler): HTTPHandler;
add(name: string, pattern?: string, page?: string): this;
add(pattern: string, page: string): this;
add(options: { name: string; pattern?: string; page?: string }): this;
Link: ComponentType<LinkProps>;
Router: Router;
}
You can find the complete file in the package at this link.
The issue I encountered was that the class definition did not include the method findAndGetUrls
, which is present in the package's default export. I attempted to extend the class definition with my own type by creating a new class like so:
import NextRoutes from 'next-routes';
class Routes implements NextRoutes {
findAndGetUrls(
nameOrUrl: string,
params: {
[key: string]: string;
},
): void;
}
However, this approach resulted in an error:
Function implementation is missing or not immediately following the declaration.
UPDATE
I decided to address this issue by creating a typings/next-routes.d.ts
file within my project with the following content:
import NextRoutes from 'next-routes';
declare module 'next-routes' {
class Routes extends NextRoutes {
findAndGetUrls(
nameOrUrl: string,
params: {
[key: string]: string;
},
): void;
}
export = Routes;
}
While this resolved the previous error regarding findAndGetUrls, it generated a new error stating that none of the other methods exist in the class, leading to the message:
Property 'add' does not exist on type 'Routes'.