简体   繁体   English

Vscode无法与Sailsjs服务和打字稿一起使用

[英]Vscode not working with Sailsjs Services and typescript

I've have a problem with typescript in a sailsjs app. 我在sailsjs应用程序中遇到打字稿问题。

I'm using typescript to write my controller, and I want to use sailsjs services inside my controllers but I get a syntax error in VSCODE. 我正在使用Typescript编写控制器,并且想在控制器内部使用sailsjs服务,但在VSCODE中遇到语法错误。

MyController.ts MyController.ts

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

declare var sails: any;

var count : number = 0;

export function stats(req:any, res:any, next: Function):any {
  console.log('TsController.ts');
  TestLog.send();

  res.status(200).send({});
}

TestLog.js TestLog.js

module.exports = {
    send: function(){
      console.log("Service is Logging")
    }
  }

But VSCODE raise an error 但是VSCODE引发一个错误

[ts] Cannot find name 'TestLog'. [ts]找不到名称“ TestLog”。

By the way if I lift the application is working as expected 顺便说一句,如果我举起应用程序按预期工作

It is because TypeScript doesn't know that you have a TestLog - it is defined in a JavaScript file and isn't imported. 这是因为TypeScript不知道您具有TestLog它是在JavaScript文件中定义的,并且未导入。

Your "MVP" fix for this is to tell the compiler not to worry about it: 为此,您的“ MVP”修复程序是告诉编译器不要担心:

declare var TestLog: any;

You can then refine this definition to make it more useful... 然后,您可以优化此定义以使其更有用...

declare var TestLog: { send(): void };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM