Sails supports using TypeScript to write your custom app code (like actions and models). You can enable this support in just a few steps:
npm install typescript ts-node --save
in your app folder.npm install @types/node --save
npm install @types/express --save
app.js
file:require('ts-node/register');
config/extensions.js
file to your project with:module.exports.moduleloader = {
sourceExt: ['js', 'ts'],
configExt: ['js', 'ts']
};
node app.js
instead of sails lift
.Here's an example Typescript controller to get you started, courtesy of @oshatrk:
Remember to name it with .ts
extension: api/controllers/TsController.ts
// api/controllers/TsController.ts
import util = require('util');
import express = require('express');
declare var sails: any;
export function index(req:any, res:any, next: Function):any {
console.log('index() from TsController.ts');
res.status(200).send('Hello from Typescript!');
}
export function config(req: express.Request, res: express.Response, next: Function) {
console.log('config() from TsController.ts');
res.status(200)
.send('<h1>sails.config :</h1><pre>' + util.inspect(sails.config) + '<pre>');
}