将TypeScript与Sails一起使用

时间:2015-01-12 01:53:09

标签: typescript sails.js

是否有人使用带有Sails的TypeScript?如果是这样,你用什么外部声明?

我正处于Sails应用程序项目的中间,并了解了TypeScript。我试图确定TypeScript是否应该为这个项目追求。任何信息将不胜感激。感谢。

2 个答案:

答案 0 :(得分:7)

来自:https://github.com/balderdashy/sails Sails是使用node connect expresssocket.io构建的。这意味着您需要这些库的定义文件。所有这些都可以从Definitive TypeScript定义存储库中获得:https://github.com/borisyankov/DefinitelyTyped

答案 1 :(得分:0)

typings.json

{
  "dependencies": {
    "bluebird": "registry:npm/bluebird#3.5.0+20170314181206",
    "connect": "registry:dt/connect#3.4.0+20160510010627",
    "express": "registry:dt/express#4.0.0+20170118060322",
    "express-serve-static-core": "registry:dt/express-serve-static-core#4.0.0+20170324160323",
    "sails": "registry:npm/sails#0.12.0+20160610190623",
    "serve-static": "registry:dt/serve-static#1.7.1+20161128184045"
  },
  "globalDependencies": {
    "es6-shim": "registry:dt/es6-shim#0.31.2+20160726072212",
    "node": "registry:dt/node#7.0.0+20170322231424",
    "socket.io": "registry:dt/socket.io#1.4.4+20170313110830"
  }
}

欢迎控制器

   /**
     * WelcomeController.ts
     *
     * @description :: Server-side logic for managing Welcomes in TS
     * @help        :: See http://links.sailsjs.org/docs/controllers
     */

    import e = require('express');
    import util = require('util');

    declare const sails: any;

    const WelcomeController = {
      index: function (req: e.Request, res: e.Response, next: Function) {
        console.log('index() from WelcomeController.ts');
        sails.models.welcome.find().limit(1).then((welcome) => {
          /// TODO: add logger
          console.log(`welcome page rendering w/ message ${welcome[0].message}`);
          return res.render('welcome', {
            welcome: welcome[0].message
          });
        }).catch((err:Error) => {
          console.error(err.message);
          return res.render('500', err)
        });


      },
      config: function (req: e.Request, res:e.Response, next:Function) {
        console.log('config() from WelcomeController.ts');
        return res.status(200)
          .send('<h1>sails.config :</h1><pre>' + util.inspect(sails.config) + '<pre>');
      }
    };

    module.exports = WelcomeController;

模型

export class Welcome {
   attributes: any = {
    id: {
      type: 'integer',
      primaryKey: true
    },
    message: {
      type: 'string',
      required: true,
      defaultsTo: 'default message'
    }
  };
}

查看

&#13;
&#13;
<div class="default-page">
  <div class="header">
    <h1 id="main-title" class="container"></h1>
    <h3 class="container">Message: <code><%= welcome %></code></h3>
  </div>
  <div class="main container clearfix">
&#13;
&#13;
&#13;

等等......我在git上开始了一个示例项目,但从未完成:

https://github.com/aslanvaroqua/sails-ts