2016-10-11 9 views
2

がtypescriptで動作するのに問題があります。 @types/sinon-stub-promiseの定義ファイルには、デフォルトのエクスポートがありません。私は密接にdocs on declaration mergingを追跡しましたが、コンパイルはしません。sinonsの入力を拡張できません

コンパイルエラー:

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の定義ファイル:

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

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タイピングを調整

最小限のサンプルレポ:https://github.com/marvinhagemeister/typings-bug

答えて

1

問題がsinonためのタイピングが@types/sinon/index.d.tsファイルの一番最後に、トップレベルにexportを持っていることです:

それが不可能な環境を使用して拡張するために作るように思わ
declare var Sinon: Sinon.SinonStatic; 

export = Sinon; 

モジュール。

しかし、すべてのモジュールを外部にしても問題ありませんが、入力の仕方をわかりません(逆に@typesとして配布することは可能です)。

(これはあなたが外部sinonモジュールの拡張子を宣言する方法です - すべてのインターフェイスがマージされます)あなたのindex.d.tsの隣sinon.d.tsファイルでこれを入れて:

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

生成されたJavaScriptコードがよさそうだ。これまでのあなたの助けのための

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

感謝を。残念ながら、私はまだ同じエラーが表示されます: 'index.ts(6,35):error TS2307: 'sinon-stub-promise'モジュールを見つけることができません。 ' – marvinhagemeister

+0

エラーなしでコンパイルする例のレポです。https://github.com/架空の/入力 - バグ – artem

+0

恐ろしい、 '* .d.ts'ファイルを直接インポートできることを知らなかった! – marvinhagemeister

関連する問題