繁体   English   中英

Typescript 编译器工作,但 Javascript 结果错误

[英]Typescript compiler work but Javascript result is wrong

首先,我想向您展示我的目录树:

\__ root
     \__ node_modules
     \__src
          \__ dir1
               \__ index.ts
               \__ package.json
               \__ deploy.config //not important here

          \__ dir2 //architecture like dir1
          \__ dir3
          \__ ...

     \__ package.json
     \__ //some configuration files

我试图通过添加 Typescript 在我的 NodeJS 项目中添加类型。 为此,我在文件夹中安装了很多东西:

  1. npm i -D typescript
  2. npm i -D @types/node
  3. npm i -D @types/express

dir1文件夹中查看index.ts脚本:

'use strict'

const toolPath = process.env.TOOLPATH || '';

import express, { Response, Request } from 'express';

var app = express();

app.get('/test', async (req: Request, res: Response) => {
    res.send("Hello world !");
});

module.exports = {
    app
};

为了测试 typescript 编译器,我在dir1文件夹中使用了以下命令: npx tsc index.ts并查看以下结果:

index.ts(9,28): error TS1005: ',' expected.
index.ts(9,42): error TS1005: ',' expected.
index.ts(9,54): error TS1005: ',' expected.
index.ts(10,8): error TS1005: ':' expected.
index.ts(10,30): error TS1005: ',' expected.

我不知道为什么我在 index.ts 中有一些错误

创建了一个index.js文件,但它包含错误:

'use strict';
var toolPath = process.env.TOOLPATH || '';
var express_1 = require('express');
var app = express_1["default"]();
app.get('/test', async(req, express_1.Request, res, express_1.Response), {
    res: .send("Hello world !")     //<--- ERROR THERE
});
module.exports = {
    app: app
};

实际上,我不知道为什么编译器不工作,但我确信这不是一个大错误。 我知道我可以使用tsconfig.json文件,但我想保留实际的项目架构。

此外,我想将index.js文件提取到当前文件夹中(如果脚本启动到dir1我想编译到dir1文件夹中)。

我想在每个src文件夹中的每个package.json中放置一个脚本来编译它,然后将我的目录部署到云中。

看到这个:

  1. npm i -D typescript
  2. npm i -D @types/node
  3. npm i -D @types/express

您只安装了 node 和 express 的类型。

通过安装这些依赖项进行修复,就像您安装了 typescript 一样;)

暂无
暂无

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

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