ts中 如何扩展koa的属性

import * as Koa from 'koa'
const app = new Koa()

app.a = 10
app.children.hong = 'hong'

我想给koa扩展这两个属性,该怎么修改呢
我试过以下方法 不管用。

1.
import * as Koa from 'koa'
declare module 'koa' {
  interface Application {
    a: number,
    children: {
      hong: string
    }
  }
}
2.
import * as Koa from 'koa'
declare module 'koa' {
  type a = number
  type children = any
}
3.
import * as KoaApplication from 'koa'
declare module 'koa' {
  export class Application {
    a: any
    children: any
  }
}
4.
import KoaApplication = require('koa')
declare module 'koa' {
  export class Application extends KoaApplication{
    a: any
    children: any
  }
}

我直接在@type下面的class Application 里面添加属性,就可以顺利编译过。但是在外面我就不知道怎么给他添加了。因为他里面是class ,我不太清楚如何在外面继承里面的class Application 然后在定义新属性。上面第4种方法尝试了,却不管用

求大神解答?

阅读 4.9k
1 个回答
declare class MyKoa extends Koa {
    a: number;
    children: {
        hong: string
    };
}

const app = new MyKoa();

app.a = 10
app.children.hong = 'hong'
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题