Adding a TypeScript Module

I am trying to increase the definition of the Sinon type for our project, here, as defined by Sinon.d.ts

 declare module 'sinon' { module Sinon { interface SinonStub extends SinonSpy { ... } interface SinonStatic { ... } ... } var Sinon: Sinon.SinonStatic; export = Sinon; } 

I have definitions.d.ts , which I use in my project for any custom definitions. Here is how I tried to do this:

 declare module 'sinon' { module Sinon { interface SinonPromise { resolves(value?: any): void; rejects(value?: any): void; } interface SinonStub { returnsPromise(): SinonPromise; } } } 

But the compiler does not recognize the new returnsPromise in the SinonStub interface and does not recognize the new type of SinonPromise .

What is wrong with this definition?

+6
source share
1 answer

I believe your case requires a workaround. The definition file you have does not have export definitions of any type, so they cannot be extended outside of this file.

I assume you installed sinon through typings install sinon . If you do typings search sinon , there are actually 2, one from npm and one from dt . By default, npm definitions are set. This dt definition is as follows:

 declare namespace Sinon { // ... interface SinonStub extends SinonSpy { // ... } // ... } declare var sinon: Sinon.SinonStatic; declare module "sinon" { export = sinon; } 

There is also a dt entry for sinon-stub-promise , which goes well with the above:

 declare namespace Sinon { interface SinonPromise { resolves(value?: any): void; rejects(value?: any): void; } interface SinonStub { returnsPromise(): SinonPromise; } } 

So this is a workaround:

  • Delete current sinon .
  • Install TyptyTyped typings for sinon : typings install sinon --source dt --global
  • Set TyptyTyped typings for sinon-stub-promise : typings install sinon-stub-promise --source dt --global

Now this compiles successfully:

 /// <reference path="typings/index.d.ts" /> import * as sinon from 'sinon'; sinon.stub().returnsPromise(); 
0
source

All Articles