Edit Page

Using TypeScript in a Sails app

Sails supports using TypeScript to write your custom app code (like actions and models). You can enable this support in just a few steps:

  1. Run npm install typescript ts-node --save in your app folder.
  2. Install the necessary typings for your app. At the very least you'll probably want to:
    npm install @types/node --save
    npm install @types/express --save
    
  3. Add the following line at the top of your app's app.js file:
    require('ts-node/register');
    
  4. Add a config/extensions.js file to your project with:
    module.exports.moduleloader = {
      sourceExt: ['js', 'ts'],
      configExt: ['js', 'ts']
    };
    
  5. Start your app with 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>');
}

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails-docs repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Sails logo
  • Home
  • Get started
  • Support
  • Documentation
  • Documentation

For a better experience on sailsjs.com, update your browser.

Documentation

Reference Concepts App structure | Upgrading Contribution guide | Tutorials More

Tutorials

  • Using CoffeeScript
  • Using TypeScript

Built with Love

The Sails framework is maintained by a web & mobile studio in Austin, TX, with the help of our contributors. We created Sails in 2012 to assist us on Node.js projects. Naturally we open-sourced it. We hope it makes your life a little bit easier!

Sails:
  • What is Sails?
  • Treeline IDE
  • Contribute
  • Logos/artwork
About:
  • The Sails Company
  • Security
  • News
  • Legal
Help:
  • Get started
  • Documentation
  • Docs
  • Enterprise
  • Hire us

© 2012-2018 The Sails Company. 
The Sails framework is free and open-source under the MIT License.