Is there a way to customize third-party declaration files?
For instance, I am looking to enhance Context
from @types/koa by adding a new field (resource
) to it.
I attempted the following:
// global.d.ts
declare namespace koa {
interface Context {
resource: any;
}
}
However, it did not succeed:
error TS2339: Property 'resource' does not exist on type 'Context'.
Update
Here is a simplified version of the code that causes this error:
import {Context} from 'koa';
import User from './Models/User';
class Controller {
async list(ctx: Context) {
ctx.resources = await User.findAndCountAll();
ctx.body = ctx.resources.rows;
ctx.set('X-Total-Count', ctx.resources.count.toString());
ctx.status = 200;
}
}
typescript v2.4
// tsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"noImplicitAny": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},
"exclude": [
"node_modules"
]
}