typescript - 无法扩展 sinon 的类型

标签 typescript sinon typescript-typings

我无法获取 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;
}

索引.ts

import * as sinon from 'sinon';
import sinonStubPromise = require('sinon-stub-promise');

sinonStubPromise(sinon);

最小样本 repo :https://github.com/marvinhagemeister/typings-bug

最佳答案

问题是 sinon 的输入有export在顶层,在 @types/sinon/index.d.ts 的最后文件:

declare var Sinon: Sinon.SinonStatic;

export = Sinon;

这似乎无法使用环境模块进行扩展。

但是,如果您也将所有模块都放在外部,这是可行的,但是我不知道如何让它与类型一起工作(另一方面,将其分发为 @types 似乎是可能的)。

把它放在 sinon.d.tsindex.d.ts 旁边的文件(这是您为外部 sinon 模块声明扩展的方式 - 所有接口(interface)都将被合并):

import "sinon";
declare module "sinon" {

    export interface SinonPromise {
        resolves(value?: any): void;
        rejects(value?: any): void;
    }

    export interface SinonStub {
        returnsPromise(): SinonPromise;
    }

    // had to add these from SinonSandbox to SinonStatic
    // 
    // no idea if it's really supposed to have those methods
    export interface SinonStatic {
        requests: SinonFakeXMLHttpRequest;
        server: SinonFakeServer;
        useFakeServer(): SinonFakeServer;
        restore(): void;
    }
}

把它放在 sinon-stub-promise.d.tsindex.ts 旁边的文件:

import * as sinon from 'sinon';

declare function setup(sinon: sinon.SinonSandbox): void;

export = setup;

然后,在您的 index.ts

// in order to use your extension you have to import both here
import * as sinon from "sinon";
import "./sinon";


import sinonStubPromise = require("./sinon-stub-promise");

sinonStubPromise(sinon);

const mkdirStub = sinon.stub()
  .returnsPromise().resolves(null);

生成的 javascript 代码看起来不错:

"use strict";
var sinon = require("sinon");
var sinonStubPromise = require("sinon-stub-promise");
sinonStubPromise(sinon);
var mkdirStub = sinon.stub()
    .returnsPromise().resolves(null);

关于typescript - 无法扩展 sinon 的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39987857/

相关文章:

javascript - Typescript 未触发名为 "name"的未初始化对象属性的错误

typescript - 为什么这 strip 有参数的路由在 Angular2 中不起作用?

javascript - 在这种特定情况下,if-else 是否比 try-catch 更好,性能明智?哪种方式是最佳做法?

typescript - 原始值可以是 Typescript 中的类型吗?

javascript - 空 Guid 字符串在 Angular/TypeScript 中显示为 0

javascript - 无法调用 res.send()

javascript - 创建 Stripe 的 Mock 还是在 Stripe 对象上使用 Stub?

node.js - Stubbing Fetch Call - response.json 不会调用

typescript - 在 typescript 库中使用未知类型是否会强制所有依赖者使用 typescript 3.0 或更高版本?

typescript - 字符串的 `Union`和 `string`可以实现吗?