繁体   English   中英

无法扩展sinon的类型

[英]Unable to extend typings of sinon

我很难让sinon-stub-promise使用打字稿。 @types/sinon-stub-promise的定义文件缺少默认导出。 我遵循关于声明合并文档,但它没有编译。

编译错误:

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''. 

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);

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

问题是sinon@types/sinon/index.d.ts文件的最末端有顶级export

declare var Sinon: Sinon.SinonStatic;

export = Sinon;

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

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

将这个在sinon.d.ts文件旁边的index.d.ts (这是你如何申报外部扩展sinon模块-所有接口将被合并):

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.ts文件旁边的index.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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM