Using TypeScript with Sails

前端 未结 2 1711
無奈伤痛
無奈伤痛 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

    from : https://github.com/balderdashy/sails Sails is built with node connect express and socket.io. This means that definition files for these libraries are what you need. All of these are available from the Definitive TypeScript definition repository : https://github.com/borisyankov/DefinitelyTyped.

    0 讨论(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"
      }
    }
    

    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('<h1>sails.config :</h1><pre>' + util.inspect(sails.config) + '<pre>');
          }
        };
    
        module.exports = WelcomeController;
    

    model

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

    View

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

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

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

    0 讨论(0)
提交回复
热议问题