N
N
Nikita Sokolov2020-01-24 18:44:04
typescript
Nikita Sokolov, 2020-01-24 18:44:04

How to export from a closure in TypeScript?

I'm making a typescript plugin, I need to create a closure so as not to break someone else's code by accident. Without a closure, it exports normally:

class Model {
    settings = {
      min: 0,
      max: 100
    };

    constructor(options: Object) {
      this.settings = $.extend(this.settings, options);
    }

    static pow(num: number, pow: number): number {
      return num ** pow;
    }
  }

  export default Model;

But with a closure it says: An export assignment can only be used in a module.
(function($: any): void {
  class Model {
    settings = {
      min: 0,
      max: 100
    };

    constructor(options: Object) {
      this.settings = $.extend(this.settings, options);
    }

    static pow(num: number, pow: number): number {
      return num ** pow;
    }
  }

  export default Model;
})(jQuery);

How nice to solve it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abberati, 2020-01-24
@wb_by

What you call a closure is the "module" pattern from the bearded old days, implemented through a closure. Your code is written in an ES2015 module, which eliminates the need for ancient "modules". Read how modules work. To solve your problem, you need a lot of leading questions and text - I'm too lazy)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question