Using TypeScript with Sails

前端 未结 2 1750
無奈伤痛
無奈伤痛 2021-02-07 21:41

Is anyone using TypeScript with Sails? If so, what are you using for external declarations?

I am in the middle of a Sails application project and learned about TypeScri

2条回答
  •  不要未来只要你来
    2021-02-07 21:58

    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"
      }
    }
    

    welcome controller

       /**
         * 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('

    sails.config :

    ' + util.inspect(sails.config) + '
    ');
          }
        };
    
        module.exports = WelcomeController;
    

    model

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

    View

    Message: <%= welcome %>

    and so on... I started an example project on git but never finished:

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

提交回复
热议问题