我正在尝试为我们的项目增加Sinon类型定义,这里是如何Sinon.d.ts
定义的
declare module 'sinon' {
module Sinon {
interface SinonStub extends SinonSpy {
...
}
interface SinonStatic { ... }
...
}
var Sinon: Sinon.SinonStatic;
export = Sinon;
}
Run Code Online (Sandbox Code Playgroud)
我有一个definitions.d.ts
我在我的项目中使用的任何自定义定义.这是我试图这样做的方式:
declare module 'sinon' {
module Sinon {
interface SinonPromise {
resolves(value?: any): void;
rejects(value?: any): void;
}
interface SinonStub {
returnsPromise(): SinonPromise;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是编译器无法识别新returnsPromise
的SinonStub
界面,也不承认新的SinonPromise
类型.
这个定义有什么问题?
我相信你的情况需要一个解决方法。您拥有的定义文件没有export
任何类型定义,因此它们不能扩展到该文件之外。
我猜你是sinon
通过安装的typings install sinon
。如果你这样做的话,typings search sinon
实际上有 2 个,一个来自npm
,一个来自dt
。默认安装定义npm
。dt
定义如下:
declare namespace Sinon {
// ...
interface SinonStub extends SinonSpy {
// ...
}
// ...
}
declare var sinon: Sinon.SinonStatic;
declare module "sinon" {
export = sinon;
}
Run Code Online (Sandbox Code Playgroud)
还有一个sinon-stub-promisedt
条目,它与上面的内容配合得很好:
declare namespace Sinon {
interface SinonPromise {
resolves(value?: any): void;
rejects(value?: any): void;
}
interface SinonStub {
returnsPromise(): SinonPromise;
}
}
Run Code Online (Sandbox Code Playgroud)
所以,这是解决方法:
sinon
输入的内容。sinon
:typings install sinon --source dt --global
sinon-stub-promise
:typings install sinon-stub-promise --source dt --global
现在可以成功编译:
/// <reference path="typings/index.d.ts" />
import * as sinon from 'sinon';
sinon.stub().returnsPromise();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
685 次 |
最近记录: |