A2MetalCore
A2MetalCore

Reputation: 1639

How to use TypeScript with Loopback

I'm using Loopback from Strongloop as a REST framework and ORM. I want to use TypeScript for my business logic. However, Loopback requires JavaScript with a specific shape to support their framework. For example:

module.exports = function(Person){

    Person.greet = function(msg, cb) {
      cb(null, 'Greetings... ' + msg);
    }

    Person.remoteMethod(
       'greet', 
        {
          accepts: {arg: 'msg', type: 'string'},
          returns: {arg: 'greeting', type: 'string'}
        }
    );
}; 

What is the TypeScript code that will generate the above JavaScript code?

Upvotes: 9

Views: 6166

Answers (1)

basarat
basarat

Reputation: 276235

What is the TypeScript code that will generate the above JavaScript code?

You can just use this code as it is (JavaScript is TypeScript). If you are curious about module.export you can use TypeScript's --module commonjs compile flag to get that in a Typeaware manner like this:

function personMixin(Person){
    Person.greet = function(msg, cb) {
      cb(null, 'Greetings... ' + msg);
    }

    Person.remoteMethod(
       'greet', 
        {
          accepts: {arg: 'msg', type: 'string'},
          returns: {arg: 'greeting', type: 'string'}
        }
    );
};

export = personMixin; // NOTE!

Here is a tutorial on TypeScript module patterns : https://www.youtube.com/watch?v=KDrWLMUY0R0&hd=1

Upvotes: 9

Related Questions