How to use TypeScript with Loopback

≯℡__Kan透↙ 提交于 2019-12-12 08:33:46

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/27572943/how-to-use-typescript-with-loopback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!