diff --git a/src/application.ts b/src/application.ts index e6c4500b4..9ba14d7c6 100644 --- a/src/application.ts +++ b/src/application.ts @@ -27,7 +27,7 @@ export type MiddlewareFunc = _MiddlewareFunc & { * Expose `Application` class. * Inherits from `Emitter.prototype`. */ -export class Application extends Emitter { +export class Application extends Emitter { [key: symbol]: unknown; /** * Make HttpError available to consumers of the library so that consumers don't @@ -41,14 +41,14 @@ export class Application extends Emitter { proxyIpHeader: string; maxIpsCount: number; protected _keys?: string[]; - middleware: MiddlewareFunc[]; - ctxStorage: AsyncLocalStorage; + middleware: MiddlewareFunc[]; + ctxStorage: AsyncLocalStorage; silent: boolean; - ContextClass: ProtoImplClass; + ContextClass: ProtoImplClass; context: AnyProto; - RequestClass: ProtoImplClass>; + RequestClass: ProtoImplClass; request: AnyProto; - ResponseClass: ProtoImplClass>; + ResponseClass: ProtoImplClass; response: AnyProto; /** @@ -84,11 +84,11 @@ export class Application extends Emitter { this.middleware = []; this.ctxStorage = getAsyncLocalStorage(); this.silent = false; - this.ContextClass = class ApplicationContext extends Context {} as ProtoImplClass; + this.ContextClass = class ApplicationContext extends Context {} as ProtoImplClass; this.context = this.ContextClass.prototype; - this.RequestClass = class ApplicationRequest extends Request {} as ProtoImplClass>; + this.RequestClass = class ApplicationRequest extends Request {} as ProtoImplClass; this.request = this.RequestClass.prototype; - this.ResponseClass = class ApplicationResponse extends Response {} as ProtoImplClass>; + this.ResponseClass = class ApplicationResponse extends Response {} as ProtoImplClass; this.response = this.ResponseClass.prototype; } @@ -151,7 +151,7 @@ export class Application extends Emitter { /** * Use the given middleware `fn`. */ - use(fn: MiddlewareFunc) { + use(fn: MiddlewareFunc) { if (typeof fn !== 'function') throw new TypeError('middleware must be a function!'); const name = fn._name || fn.name || '-'; if (isGeneratorFunction(fn)) { @@ -160,7 +160,7 @@ export class Application extends Emitter { 'https://github.com/koajs/koa/blob/master/docs/migration.md'); } debug('use %o #%d', name, this.middleware.length); - this.middleware.push(fn); + this.middleware.push(fn as MiddlewareFunc); return this; } @@ -196,7 +196,7 @@ export class Application extends Emitter { * Handle request in callback. * @private */ - protected async handleRequest(ctx: T, fnMiddleware: (ctx: T) => Promise) { + protected async handleRequest(ctx: Context, fnMiddleware: (ctx: Context) => Promise) { this.emit('request', ctx); const res = ctx.res; res.statusCode = 404; @@ -246,7 +246,7 @@ export class Application extends Emitter { /** * Response helper. */ - protected _respond(ctx: T) { + protected _respond(ctx: Context) { // allow bypassing koa if (ctx.respond === false) return; diff --git a/src/request.ts b/src/request.ts index e44a9f23f..10ad52e7c 100644 --- a/src/request.ts +++ b/src/request.ts @@ -16,16 +16,16 @@ export interface RequestSocket extends Socket { encrypted: boolean; } -export class Request { +export class Request { [key: symbol]: unknown; app: Application; req: IncomingMessage; res: ServerResponse; - ctx: T; + ctx: Context; response: Response; originalUrl: string; - constructor(app: Application, ctx: T, req: IncomingMessage, res: ServerResponse) { + constructor(app: Application, ctx: Context, req: IncomingMessage, res: ServerResponse) { this.app = app; this.req = req; this.res = res; diff --git a/src/response.ts b/src/response.ts index d070fe6fd..476093fbf 100644 --- a/src/response.ts +++ b/src/response.ts @@ -16,15 +16,15 @@ import type { Application } from './application.js'; import type { Context } from './context.js'; import type { Request } from './request.js'; -export class Response { +export class Response { [key: symbol]: unknown; app: Application; req: IncomingMessage; res: ServerResponse; - ctx: T; + ctx: Context; request: Request; - constructor(app: Application, ctx: T, req: IncomingMessage, res: ServerResponse) { + constructor(app: Application, ctx: Context, req: IncomingMessage, res: ServerResponse) { this.app = app; this.req = req; this.res = res; diff --git a/test/application/context.test.ts b/test/application/context.test.ts index 27087598b..4078f5da1 100644 --- a/test/application/context.test.ts +++ b/test/application/context.test.ts @@ -47,7 +47,7 @@ describe('app.context', () => { } } - class MyApp extends Application { + class MyApp extends Application { constructor() { super(); this.ContextClass = MyContext; @@ -55,7 +55,7 @@ describe('app.context', () => { } const app = new MyApp(); - app.use(ctx => { + app.use((ctx: MyContext) => { ctx.body = `hello, ${ctx.getMsg()}`; });