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?
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.
A module is designed with the idea to organize code written in TypeScript. Modules are broadly divided into −. Internal Modules. External Modules.
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.
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 ...
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:
sinon
typing.sinon
: typings install sinon --source dt --global
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With