简体   繁体   English

如何将现有的节点应用程序转换为打字稿?

[英]How do I convert my existing node application to typescript?

I have a functioning node project. 我有一个正常运行的节点项目。 It was written using javascript. 它是使用javascript编写的。 The app is pretty simple (mostly, it was just hosting a static folder). 该应用程序非常简单(大多数情况下,它只是托管一个静态文件夹)。 Some of the web pages it was hosting are successfully using typescript for client javascript. 它托管的某些网页已成功将Typescript用于客户端javascript。 Now I'm trying to convert my node to typescript (I'm using Visual Studio Code as an IDE), and I'm having a lot of weird errors, and it's hard to find out what I'm doing wrong as the error messages aren't coming up in any good searches. 现在,我正在尝试将节点转换为打字稿(我使用Visual Studio Code作为IDE),并且遇到了许多奇怪的错误,并且很难找出我做错了什么错误任何好的搜索都不会显示邮件。

I downloaded the .d.ts files from definitely typed and put it into a typescript folder, then I renamed my index.js file to index.ts , and added a reference to it in the tasks.json file so that it would compile the new typescript file. 我从绝对类型下载了.d.ts文件,并将其放入.d.ts文件夹中,然后将index.js文件重命名为index.ts ,并在tasks.json文件中添加了对它的引用,以便可以编译新的打字稿文件。 Then I added the typescript stuff to the top of my index.js 然后,将打字稿内容添加到index.js的顶部

///<reference path='ts/express.d.ts' />

import * as express from "express";
var app = express();
var path = require('path');

When I did that, I got these error messages 当我这样做时,我收到了这些错误消息

C:/src/www/ts/node.d.ts (128,29): Expected ';'
C:/src/www/ts/mime.d.ts (17,12): Expected identifer after 'import'
C:/src/www/ts/mime.d.ts (27,12): Expected identifer after 'import'
C:/src/www/ts/mime.d.ts (3,8): Expected identifer after 'import'

Is there something wrong with the .d.ts files? .d.ts文件有问题吗?

My typescript isn't as good as the guys who wrote the d.ts files, I'm sure, and this line looks right: 我敢肯定,我的打字稿不如编写d.ts文件的人好,这一行看起来很正确:

isBuffer(obj: any): obj is Buffer;

but is the line that generates the Expected ';' 但是是生成Expected ';' error, but I don't really trust that, because the other three error messages occur in mime.d.ts which doesn't contain the word import . 错误,但我并不十分相信,因为其他三个错误消息出现在mime.d.ts ,其中不包含单词import

So, I thought I'd start smaller, and created a separate JS file. 因此,我以为我会做得较小,并创建了一个单独的JS文件。 It just contains: 它只包含:

import * as express from "express";

And that generates the same error message. 并生成相同的错误消息。

C:/src/www/simple.ts (1,8): Expected identifer after 'import'

What am I doing wrong? 我究竟做错了什么?

-- Updates -- - 更新 -

This is my tsconfig 这是我的tsconfig

{ "compilerOptions": {
    "target": "es5",
    "module": "amd",
    "sourceMap": true    
}}

You're using typescript 0.8.3, which is very outdated. 您使用的打字稿0.8.3非常过时。 It may be (or not) the solution, but it's a good start to keep it up to date. 这可能是(也可能不是)解决方案,但这是使它保持最新状态的一个好的开始。 I'm using Atom as my IDE, so I've installed typescript with npm, which always resolve to latest version. 我将Atom用作我的IDE,所以我在npm上安装了打字稿,该打字稿始终解析为最新版本。 Don't know about VS Code. 不了解VS Code。

About your .tsconfig , ideally you should be using typings to manage .d.ts files. 关于您的.tsconfig ,理想情况下,你应该使用分型管理.d.ts文件。 It will download and organize all typings definition in a single directory with a single entry point (called main.d.ts ). 它将在具有单个入口点(称为main.d.ts )的单个目录中下载和组织所有类型定义。 This file and all others .ts files are then listed on files property of your .tsconfig file, so the compiler knows which files to use. 然后,此文件和所有其他.ts文件会列在.tsconfig文件的files属性上,因此编译器知道要使用哪些文件。

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

相关问题 如何将自定义 static 方法添加到 TypeScript 中的现有节点 class(例如缓冲区)? - How do I add a custom static method to an existing node class (e.g. Buffer) in TypeScript? 如何将我的对象转换为 node.js 中的数组? - How do I convert my objects to an array in node.js? 如何将我的节点快速应用程序限制为仅本地主机? - How do i restrict my node express application to localhost only? 如何在应用程序中使用.Node文件 - How do I use .Node files in my Application 使用AWS OpsWorks,如何启动节点应用程序? - Using AWS OpsWorks, how do I start my node application? 如何使用Node EJS将Web应用程序转换为具有相同功能的桌面应用程序 - How I can convert my web application to desktop application with same functionality using Node EJS 如何将运行在应用程序服务器(即JBOSS)上的现有Web应用程序与node.js集成在一起? - how can I integrate my existing web application running on application server(i.e.JBOSS) with node.js? 如何将我的 Typescript Node.js 应用程序部署到 Heroku? - How do I deploy my Typescript Node.js app to Heroku? 如何将 Node.js 的 TypeScript 代码编译为一个文件? - How do I compile my TypeScript code for Node.js to one file? 如何在节点 typescript 中将 blob 转换为 base64 - How can I convert an blob to base64 in node typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM