【发布时间】:2017-02-20 14:54:39
【问题描述】:
我无法让 sinon-stub-promise 使用 typescript。 @types/sinon-stub-promise 中的定义文件缺少默认导出。我密切关注docs on declaration merging,但它没有编译。
编译错误:
index.ts(4,18): error TS2345: Argument of type 'typeof 'sinon'' is not assignable to parameter of type 'SinonSandbox'.
Property 'clock' is missing in type 'typeof 'sinon''.
index.ts(6,25): error TS2339: Property 'stub' does not exist on type 'typeof 'sinon''.
诗乃的定义文件:
declare namespace Sinon {
// ...
interface SinonStubStatic {
(): SinonStub;
(obj: any): SinonStub;
(obj: any, method: string): SinonStub;
(obj: any, method: string, func: any): SinonStub;
}
// ...
interface SinonFakeTimers {
now: number;
create(now: number): SinonFakeTimers;
setTimeout(callback: (...args: any[]) => void, timeout: number, ...args: any[]): number;
// ...
}
interface SinonSandbox {
clock: SinonFakeTimers;
// ...
stub: SinonStubStatic;
// ...
}
}
调整sinon-stub-promise 打字:
// Generated by typings
// Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/7de6c3dd94feaeb21f20054b9f30d5dabc5efabd/sinon-stub-promise/sinon-stub-promise.d.ts
/// <reference path="../../../node_modules/@types/sinon/index.d.ts" />
declare module 'sinon' {
interface SinonPromise {
resolves(value?: any): void;
rejects(value?: any): void;
}
interface SinonStub {
returnsPromise(): SinonPromise;
}
}
// Added by me, because this is missing in the typings on dt
declare module 'sinon-stub-promise' {
function setup(sinon: Sinon.SinonSandbox): void;
export = setup;
}
index.ts
import * as sinon from 'sinon';
import sinonStubPromise = require('sinon-stub-promise');
sinonStubPromise(sinon);
【问题讨论】:
标签: typescript sinon typescript-typings