个人gels项目(https://github.com/zhoutk/gels),几天没动,忽然tsc编译出错,信息以下:git
src/app.ts:28:38 - error TS2345: Argument of type 'any[]' is not assignable to parameter of type '[Middleware<ParameterizedContext<any, {}>>]'. Property '0' is missing in type 'any[]' but required in type '[Middleware<ParameterizedContext<any, {}>>]'. 28 m && (app.use.apply(app, [].concat(m)))
个人源代码,是动态加载Koa的中间件,app是koa2实例github
for (let m of [].concat(middleware)) { m && (app.use.apply(app, [].concat(m))) }
几天前仍是正常编译、正常运行的项目,忽然出错,应该是环境变了。经查找,发现全局typescript已经升级到了最新版本,3.2.2,而项目中的版本是3.0.3。
将全局版本换回3.0.3,编译经过,问题找到。typescript
上typescrpt的github主页,找发布日志,发现3.2的新功能,第一条就是:json
TypeScript 3.2 introduces a new --strictBindCallApply compiler option (in the --strict family of options) with which the bind, call, and apply methods on function objects are strongly typed and strictly checked.
大概意思是:TypeScript 3.2引入一个新的编译选项 --strictBindCallApply,若使用这个选项,函数对象的bind,call和apply方法是强类型的,并进行严格检测。app
由于是动态参数,想了半天我无法明确声明类型。所以,我在tsconfig.json配置文件中,设置"strictBindCallApply": false,将这个编译选项关闭,这样就能够将typescript升级到3.2.2了。
哪位朋友如有能打开strictBindCallApply选项,又能经过编译的方案,烦请告知一下,谢谢!我若哪天找到方法,会立刻更新本文章。koa