Chic
Chic

Reputation: 10489

Overwrite previously declared TypeScript function

In my IDE all of the .ts TypeScript files are compiled into a single file. This includes my unit test files. This allows me to have type checking in my tests and a build all in one step. However, when I boot my dev server it cannot find the jasmine method describe() so I'm overloading it if it's not defined.

// If jasmine functions are not defined (i.e. not running tests) skip functions
if (describe === undefined) {
  var describe = (function () { // Duplicate identifier 'describe'
    return function (description: string, callback: () => void) { return }
  })();
}

And in jasmine.d.ts

declare function describe(description: string, specDefinitions: () => void): void;

The issue is TypeScript correctly recognizes that I'm redefining an existing function but I'm intending to do this (hence the if (describe === undefined)).

Question:

Is there a way to suppress this error? I think I'm looking for some thing like a function describe() implements jasmine.describe but as far as I can tell this is only available for classes.

Note: I'm using TypeScript 1.4

Upvotes: 2

Views: 583

Answers (2)

Chic
Chic

Reputation: 10489

The most simple fix is to edit jasmine.d.ts to comment out the describe function declaration. The disadvantage of this is that you are editing a library definition and you will have to redo this every time you update the definition file.

Upvotes: 0

Paleo
Paleo

Reputation: 23682

You can use the global object.

If the code is executed in a browser:

window['describe'] = /* ... */

Or with Node.js:

global['describe'] = /* ... */

Alternatively, you can use eval.

Upvotes: 3

Related Questions