deno 1.23 から deno run 時の型チェックが無いので実行できるが、 deno check してみると次のようにエラーに! $ deno check server.ts Check file:///usr/src/app/server.ts error: TS2339 [ERROR]: Property 'customProp' does not exist on type 'Context<State, Record<string, any>>'. context.customProp = "middleware append value"; ~~~~~~~~~~ at file:///usr/src/app/server.ts:6:11 TS2339 [ERROR]: Property 'customProp' does not exist on type 'Context<State, Record<string, any>>'. console.log(context.customProp); ~~~~~~~~~~ at file:///usr/src/app/server.ts:11:23
oak 側が、拡張したプロパティに対応しないので、 型チェックエラーになる! error: TS2345 [ERROR]: Argument of type '(context: CustomContext, next: () => Promise<unknown>) => void' is not assignable to parameter of type 'Middleware<State, Context<State, Record<string, any>>>'. Types of parameters 'context' and 'context' are incompatible. Type 'Context<State, Record<string, any>>' is not assignable to type 'CustomContext'. Property 'customProp' is missing in type 'Context<State, Record<string, any>>' but required in type 'CustomProps'. app.use((context: CustomContext, next: () => Promise<unknown>) => { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ at file:///usr/src/app/server.ts:11:9 'customProp' is declared here. customProp: string; ~~~~~~~~~~