Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Augmenting a TypeScript Module

I'm trying to augment the Sinon type definition for our project, here's how the Sinon.d.ts is defined

declare module 'sinon' {
  module Sinon {
    interface SinonStub extends SinonSpy {
      ...
    }
    interface SinonStatic { ... }
    ...
  }

  var Sinon: Sinon.SinonStatic;

  export = Sinon;
}

I have a definitions.d.ts which I use in my project for any custom definitions. Here's how I tried to do it:

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, nor does it recognize the new SinonPromise type.

What's wrong with that definition?

like image 599
thitemple Avatar asked Aug 04 '16 14:08

thitemple


People also ask

What is module augmentation in typescript?

This TypeScript tutorial will cover how we can use module augmentation to solve any type errors you might encounter when working with external libraries. When you get stuck, use module augmentation to tweak the modules that you can’t access otherwise.

What are typescript modules?

A module is designed with the idea to organize code written in TypeScript. Modules are broadly divided into −. Internal Modules. External Modules.

How to migrate typescript from AMD to CommonJS?

When defining external module in TypeScript targeting CommonJS or AMD, each file is considered as a module. So it’s optional to use internal module with in external module. If you are migrating TypeScript from AMD to CommonJs module systems, then there is no additional work needed.

How do I extend a class using TypeScript?

In order to extend it, we have a declare a module using the same name and in that module, we will declare an interface with the same name as the class we are trying to extend. In the interface, we will include the properties and methods we want to add to the extended class. TypeScript will merge both the Pet class and the Pet interface ...


1 Answers

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

I'm guessing that you installed sinon via typings install sinon. If you do typings search sinon there are actually 2, one from npm and one from dt. The default installs the npm definitions. This is what the dt definition looks like:

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 plays nicely with the above:

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

  interface SinonStub {
    returnsPromise(): SinonPromise;
  }
}

So, this is the workaround:

  1. Remove the current sinon typing.
  2. Install the DefinitelyTyped typings for sinon: typings install sinon --source dt --global
  3. Install the DefinitelyTyped typings for sinon-stub-promise: typings install sinon-stub-promise --source dt --global

This now successfully compiles:

/// <reference path="typings/index.d.ts" />

import * as sinon from 'sinon';

sinon.stub().returnsPromise();
like image 91
Frank Tan Avatar answered Oct 14 '22 10:10

Frank Tan