Reputation: 415
I'm working with the koa-router
type declarations from the DefinitelyTyped repository. The class declaration does not include a definition for the opts
property, which means that attempting to access the property will produce a TS2339 error.
Aside from non-typed workarounds, e.g. (routerObj as any).opts.<option>
, how can I add/extend this property with the types from IRouterOptions to the already existing definition?
*Edit*
I tried module augmentation, but I was unsuccessful in doing so:
import Router from 'koa-router';
declare module 'koa-router' {
class Router {
opts: Router.IRouterOptions
}
}
And following the documentation bit above
import Router from 'koa-router';
declare module 'koa-router' {
interface Router<T> {
opts: Router.IRouterOptions
}
}
Neither of these worked. The error message I get from VSCode is also quite strange, which leads me to believe TS treats my attempt on augmentation and the DefinitelyTyped definitions as independent definitions: Property 'opts' does not exist on type 'import("/Absolute/Path/To/node_modules/@types/koa-router/index.d.ts")'.
However, augmenting another interface from the DefinitelyTyped definitions does work:
import Router from 'koa-router';
declare module 'koa-router' {
interface IRouterOptions {
requiredVar: boolean;
}
}
This will produce a TS2322: Type '{}' is not assignable to type 'IRouterOptions'. Property 'requiredVar' is missing in type '{}'.
Upvotes: 0
Views: 222