在节点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"
}
}
答案 0 :(得分:1)
如果定义已经将Assertion
接口全局分配给Object.should
(如果您被告知您正在进行后续声明,则必须具有该接口)...
如果您已扩展原始Assertion
界面....
然后,您不需要在全局声明中重新指定界面。
当您在同一个公共根目录中编写多个接口定义时,它们都会对单个类型产生影响。这意味着您添加的接口就像它们在同一个代码块中一样。
如果您收到错误提示,或者您无法看到您认为自己添加到界面中的类型信息,那么您将无法设法点击相同的公共根,即界面存在于X.Y.Z
中,但您已将其添加到X.Y
。