如何使用带有Loopback的TypeScript [英] How to use TypeScript with Loopback
问题描述
我使用Strongloop的Loopback作为REST框架和ORM。我想将TypeScript用于我的业务逻辑。但是,Loopback需要具有特定形状的JavaScript来支持其框架。例如:
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'}
}
);
};
什么是生成上述JavaScript代码的TypeScript代码?
What is the TypeScript code that will generate the above JavaScript code?
推荐答案
什么是生成上述JavaScript代码的TypeScript代码?
What is the TypeScript code that will generate the above JavaScript code?
您可以直接使用此代码(JavaScript是TypeScript)。如果您对 module.export
感到好奇,可以使用TypeScript的 - 模块commonjs
编译标志来获取类型识别这样的方式:
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!
这是关于TypeScript模块模式的教程: https://www.youtube.com/watch?v=KDrWLMUY0R0&hd=1
Here is a tutorial on TypeScript module patterns : https://www.youtube.com/watch?v=KDrWLMUY0R0&hd=1
这篇关于如何使用带有Loopback的TypeScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!