2017-08-10 186 views
1

我想在打字稿中使用chai,并且我无法获得任何断言以按我期望的方式工作。Typescript @ types/chai - 你如何使用Chai.Assertion?

的package.json

"dependencies": { 
    "@types/chai": "^4.0.1", 
    "@types/mocha": "^2.2.41", 
    "chai": "^4.1.0", 
    "mocha": "^3.4.2", 
    "typescript": "2.4.2" 
} 

所有这些发射编译器错误:

import * as chai from 'chai'; 
const expect = chai.expect; 
const assert = chai.assert; 

const expect = chai.expect; 
expect(myVar).to.be.empty(); 
expect(myVar).to.be.empty; 
expect(myVar).to.be.undefined(); 
expect(myVar).to.be.undefined; 

如果我尝试直接导入断言 - 我不看空()作为一个有效的断言:

const assert = chai.assert; 
assert.empty??? not there? 

欢迎任何想法。

谢谢 - 乍得


快速回答

肖恩指出如下 - 更改导入到:

import { assert, expect } from 'chai'; 

感谢肖恩

回答

1

不幸的是,该类型做不包括isEmpty(...)assert;那will probably change for 4.0.3。在此期间,我们可以投assert as any并致电isEmpty(...)

一旦你已经安装的类型,这可能会为你工作:

import chai = require("chai"); 
const assert = chai.assert; 
const expect = chai.expect; 

expect("").to.be.empty; 
expect(undefined).to.be.undefined; 
(assert as any).isEmpty(""); 

另一种方法是在进口解构:

import { assert, expect } from "chai"; 

expect("").to.be.empty; 
expect(undefined).to.be.undefined; 
(assert as any).isEmpty(""); 

这里是如何将这些看在VSCode。

enter image description here

+0

感谢肖恩 - 我明确地谈论那些类型的那些:断言的 - 像真正的:断言; false:断言; null:断言; undefined:断言; NaN:断言; NaN:断言; 存在:断言; 空:断言; ;;;;我有平等和其他工作很好 – chadbr

+0

@chadbr谢谢你让我知道。我已经相应地更新了我的答案。 –

+1

在进口解构做了诡计!它打破了tslint - 但我可以添加一个忽略。有趣的一面是,我在几个月前试过了,它不起作用 - 所以我甚至没有再尝试。谢谢Shaun。 – chadbr