打字稿编译器工作但 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 项目中添加类型。为此,我在 root 文件夹中安装了很多东西:

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

看到 index.ts 脚本到 dir1 文件夹:

'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
};

为了测试打字稿编译器,我在 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文件夹)。

我想在每个package.json文件夹下每个src放一个脚本来编译然后部署我的目录进入云端。

看到这个:

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

您只安装了 node 和 express 类型。

通过安装这些依赖项来修复,就像你已经安装了打字稿一样;)