9

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?

Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
A2MetalCore
  • 1,621
  • 4
  • 25
  • 49
  • how do you handle using loopback models in your typescript code? – JBCP Jul 19 '16 at 19:04
  • Can you be more specific.... Not sure what you're asking. – A2MetalCore Jul 19 '16 at 19:12
  • I'm just wondering since Loopback models are defined as JSON, if you've found a way to get those models mapped into TypeScript for the compiler to validate your code with. – JBCP Jul 19 '16 at 19:24
  • We're successfully using Loopback with TypeScript. We dynamically generate the REST endpoints at startup rather than using the JSON schemas. Then we have an app that generates TS interfaces for all of our models which we then 'include' in our TS code. That gives us strong typing while coding, but it does not perform validation at runtime. – A2MetalCore Jul 19 '16 at 22:30
  • Hi @ASA2 - I don't suppose the app that generates TS interfaces for your models is open source is it? Why did you choose to go JS models -> TS interface instead of creating the models in TS and generating Loopback-compatible JS? – JBCP Sep 09 '16 at 16:26
  • @JBCP ASA2 - do either of you have an example that you can share of Typescript + Loopback (specifically the models). I'm also interested in any build jobs that may exist. – roder Jan 11 '17 at 20:06
  • @roder I don't, but I could try and update our boilerplate project to show how we use TS. Unfortunately we don't do anything special with the models right now - we write them the traditional way in json and then manually write a `.d.ts` file for the ones that we need to access in TS. Its not a great solution but it works for now. I think a better solution would be to use TS code to describe the models and have that loaded by loopback – JBCP Jan 11 '17 at 20:37

1 Answers1

9

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

basarat
  • 261,912
  • 58
  • 460
  • 511
  • Thanks! That works. Also, it works without the --module commonjs. Did something change in TypeScript? Also, how would I specify those parameters in Visual Studio so they are appended automatically? – A2MetalCore Dec 21 '14 at 13:30
  • Commonjs is the default. Specify from project properties. – basarat Dec 21 '14 at 21:52