R
R
Riim2015-12-04 13:09:11
typescript
Riim, 2015-12-04 13:09:11

Typescript and ternary statement in export?

The situation is this: I check for the presence of a native Set , and if it exists, then the module returns it, if not, then the polyfill. Looks like this:

let Set = (<any>window).Set;

if (!Set || Set.toString().indexOf('[native code]') == -1) {
  Set = class Set<T> {
    // ...
  };
}

export = Set;

The problem is that when I use this module somewhere, I cannot use the Set type:
import Set = require('./Set');
let s: Set<Node> = new Set<Node>();

says he doesn't know what Set is.
If you always return the class, then everything is fine, but you need to somehow wedge in the native version. I tried to rewrite everything:
class Set<T> {
  // ...
};

let NativeSet = (<any>window).Set;

export = NativeSet && NativeSet.toString().indexOf('[native code]') != -1 ? NativeSet : Set;

that doesn't work either. Typing NativeSet in the export expression as <type<Set<T>>>
also does not work.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question