【发布时间】:2018-02-10 02:10:07
【问题描述】:
在 Node 8.9.1 上使用 TypeScript 2.7.1 进行编译时,库 should 的版本 13.2.1 会产生以下错误:
node_modules/should/should.d.ts(237,5): error TS2717: Subsequent property
declarations must have the same type. Property 'should' must be of type
'Assertion', but here has type 'Assertion'.
即指向类型定义文件中的这一行:https://github.com/shouldjs/should.js/blob/9748ae607f80fbf544d4bb67db8e1e014de2301f/should.d.ts#L237
还有一个issue in the should.js github repository,此前已发现并认为已解决。
简化的错误生成文件
以下是产生相同错误的真实文件的简化版本:
declare namespace should {
interface Assertion {
assert(expr: boolean): this;
fail(): this;
}
}
declare global {
interface Object {
should: should.Assertion;
}
}
export as namespace should;
export = should;
快速修复
一个简单的解决方案是跳过全局声明。这将满足我自己的库用例,但由于该库当前旨在允许此用例,因此它似乎不是一个理想的场景。
问题
为什么这个声明不起作用?什么才是有效的声明?
编辑
测试相关的依赖,一些感人的全局类型定义和潜在的冲突。
{
"devDependencies": {
"@types/chai": "^4.1.2",
"@types/chai-as-promised": "^7.1.0",
"@types/mocha": "^2.2.48",
"@types/proxyquire": "^1.3.28",
"@types/request-promise": "^4.1.41",
"@types/sinon": "^4.1.3",
"@types/sinon-chai": "^2.7.29",
"@types/supertest": "^2.0.0",
"chai": "^4.0.0",
"chai-as-promised": "^7.1.1",
"chai-things": "^0.2.0",
"mocha": "^3.4.2",
"nyc": "^11.4.1",
"proxyquire": "^1.8.0",
"should": "^13.2.1",
"sinon": "^4.2.2",
"sinon-chai": "^2.14.0",
"sinon-mongoose": "^2.0.2",
"supertest": "^1.2.0"
}
}
【问题讨论】:
标签: typescript types mocha.js should.js