【问题标题】:ts2339: property 'should' does not exist on type Bluebird<boolean>ts2339:Bluebird<boolean> 类型上不存在属性“应该”
【发布时间】:2018-01-14 13:41:33
【问题描述】:

我在 Typescript 中编写了 mocha 测试,现在我在 es6 中编译它

tsc *.ts --target es6 -m commonjs --watch

我在控制台中没有收到任何错误,
但是在 WebStorm 中,每个“应该”柴关键字都用红色下划线(当我将鼠标悬停时,我收到一条消息:ts2339:蓝鸟类型上不存在属性“应该”强>)。

例如,我正在使用“chai”,我想检查预期值是否为真:

import * as chai    from "chai";

export class WrapedChai {
     public shouldBeTrue(valueToTest : any){
        let expectedValue : boolean = true;
        expectedValue.should.equal(valueToTest);
    }
// or usintg SHOULD with a Promise
     public belongsToGLOErrorPromise(valueToTest: any) {
        let expectedValue   : boolean = true;
        return Promise.resolve(expectedValue).should.eventually.equal(valueToTest);
    } 
}

并且应该总是用红色下划线。

我试过这个:

import { should } from 'chai';
should();

还有这个:

import chai = require('chai');
var should = chai.should();

但“应该”仍以下划线显示为错误。

【问题讨论】:

  • 嗯,我从来没有在打字稿的上下文中使用“应该”,但“期望”工作得很好。顺便提一句。您的代码示例与测试和您后来添加的导入似乎不匹配(因为您在示例中导入“断言”)
  • 'expect' 对我不起作用(在这种情况下)。就像我说的“应该”正在工作,并且在控制台中没有错误,但它只是在 Webstorm 中被强调为“应该”在(在这种情况下)“expectedValue”或 true 或其他任何 WebStorm 上不存在的错误告诉我“应该”不存在。

标签: typescript ecmascript-6 mocha.js chai commonjs


【解决方案1】:

当前版本的 Chai 不再支持不同于 'any' 的类型。

所以你建议转换为“任何”是好的。

另外4.2.0之前的上一个版本仍然支持所有类型,所以使用上一个版本可以解决,使用没有问题:

let expectedValue : boolean = true;
expectedValue.should.equal(valueToTest);

https://github.com/chaijs/chai/issues/1100

【讨论】:

    【解决方案2】:

    这可以通过npm install @types/chai解决。

    【讨论】:

      【解决方案3】:

      好的,我解决了这个问题。

      解决问题的第一种方法(使用 hack):
      问题在于 TypeScript 中的类型。而不是写

      return Promise.resolve(isInstance).should.eventually.equal(expectedValue);
      

      我把这条线分成两部分,这样写:

      let helpVariable : any = Promise.resolve(isInstance);
      return helpVariable.should.eventually.equal(expectedValue);
      

      这样,我将应用 SHOULD 应用于任何类型的变量。

      出于某种原因,如果将“应该”应用于任何其他类型(例如:布尔、整数、数字、字符串...),则“应该”会被下划线标记为错误

      第二种解决方法(使用“export”关键字而不是“should”):
      首先像这样导入关键字'export':

        import {expect} from 'chai';
      

      那么,不要写:

      return Promise.resolve(isInstance).should.eventually.equal(expectedValue);
      

      写:

      return export(Promise.resolve(isInstance)).eventually.equal(expectedValue);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-29
        • 2016-03-14
        • 2017-02-25
        • 2021-11-24
        • 2019-01-11
        • 2021-05-08
        • 2021-10-15
        • 2020-12-13
        相关资源
        最近更新 更多