【问题标题】:Same types but still error on global declaration "TS2717: Subsequent property declarations must have the same type with same type."相同类型但全局声明仍然出错“TS2717:后续属性声明必须具有相同类型和相同类型。”
【发布时间】: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


    【解决方案1】:

    如果定义已经将Assertion 接口全局分配给Object.should(如果您被告知要进行后续声明,则必须具有该接口)...

    如果你扩展了原来的Assertion接口......

    那么你就不需要在全局声明中重新指定接口了。

    为什么?

    当您在同一个公共根目录中编写多个接口定义时,它们都属于一个类型。这意味着您对接口的添加会被包含在同一个代码块中。

    错误

    如果您收到您提到的错误,或者如果您看不到您认为添加到接口的类型信息,那将是因为您没有设法找到相同的公共根,即@中存在接口987654325@,但您已将其添加到X.Y。

    【讨论】:

    • 谢谢。基于此,我创建了一个 PR,我刚刚删除了该全局声明:github.com/shouldjs/should.js/pull/165
    • 如 PR 中所述,此错误是由于包含 @types/mocha 以及此库,因为它们都修改了全局对象接口。 should.js 最后被评估可能是错误仅指向该库而不指向 @types/mocha 的原因。这也符合这个答案。
    猜你喜欢
    • 2022-09-30
    • 2017-11-15
    • 2018-04-18
    • 1970-01-01
    • 2019-02-06
    • 2016-11-07
    • 2019-09-04
    • 1970-01-01
    • 2017-07-14
    相关资源
    最近更新 更多