简体   繁体   English

使用 Typescript 在 Visual Studio Code 中构建

[英]Building in Visual Studio Code with Typescript

I'd be absolutely impressed if anyone could help me with this problem which has spiraled out of control.如果有人能帮助我解决这个已经失控的问题,我绝对会印象深刻。

I just got a computer, so I've been installing all of my software and setting things up.我刚买了一台电脑,所以我一直在安装我所有的软件并进行设置。 On my old machine, in Visual Studio Code, I had a project that used TypeScript to compile into a single.js file that I could upload to a server (PlayFab specifically).在我的旧机器上,在 Visual Studio Code 中,我有一个项目使用 TypeScript 编译成一个可以上传到服务器(特别是 PlayFab)的 single.js 文件。 I was able to simply hit Ctrl + Shift + B and the project would build the file (assuming no compile errors, of course).我能够简单地按 Ctrl + Shift + B 并且项目将构建文件(当然假设没有编译错误)。

I don't remember how I had originally installed TypeScript onto that machine, but I'm having a ton of problems on this new one (now Windows 10).我不记得我最初是如何将 TypeScript 安装到那台机器上的,但是我在这台新机器上遇到了很多问题(现在是 Windows 10)。 I tried installing using npm install -g typescript which didn't seem to make any relevant files (specifically the tsc.exe file needed to compile?) and put whatever it did 'install' in AppData.我尝试使用npm install -g typescript进行安装,这似乎没有生成任何相关文件(特别是需要编译的 tsc.exe 文件?)并将它所做的任何“安装”放在 AppData 中。 After running that, I tried compiling in VS Code but I got errors about it not knowing what tsc was.运行之后,我尝试在 VS Code 中编译,但我得到了关于它的错误,不知道 tsc 是什么。

On my old machine, I noticed I had a TypeScript directory under C:\Program Files (x86)\Microsoft SDKs\TypeScript.在我的旧机器上,我注意到在 C:\Program Files (x86)\Microsoft SDKs\TypeScript 下有一个 TypeScript 目录。 I went back to the TypeScript site to download an installer, and sure enough I got that directory (in the same place, as well as the matching value in the Path env variable).我回到 TypeScript 站点下载安装程序,果然我得到了那个目录(在同一个地方,以及 Path 环境变量中的匹配值)。 When I try to compile now, it actually tries opening up the file with another application?, I hastily selected Windows Script Host.当我现在尝试编译时,它实际上是在尝试用另一个应用程序打开文件?我匆忙选择了 Windows Script Host。 and now I just get some random error when it tries opening the file, I went into Default Programs and made it Visual Studio Community, just to see what would happen, and still when I hit Ctrl + Shift + B to compile.现在我在尝试打开文件时遇到了一些随机错误,我进入默认程序并将其设为 Visual Studio 社区,只是想看看会发生什么,当我按下 Ctrl + Shift + B 进行编译时。 it opens up Visual Studio, I have no idea why on Earth using the shortcut to compile (I checked in settings, it is the Ctrl + Shift + B) would attempt to open the file in another application!它打开了 Visual Studio,我不知道为什么在地球上使用快捷方式编译(我检查了设置,它是 Ctrl + Shift + B)会尝试在另一个应用程序中打开文件!

So right now my problem is 2-fold.所以现在我的问题是2倍。 A) Have the Build shortcut attempt to build instead of opening another application, and B) Have the TypeScript successfully instead and transpiled so I can produce my.js file. A) 让 Build 快捷方式尝试构建而不是打开另一个应用程序,并且 B) 让 TypeScript 成功转译,以便我可以生成 my.js 文件。

I know this was a wordy question, but it's complicated and really causing me a lot of trouble.我知道这是一个冗长的问题,但它很复杂,确实给我带来了很多麻烦。 If anyone can provide any helpful insight, I'm all ears!如果有人可以提供任何有用的见解,我全神贯注! Please and thank you!谢谢,麻烦您了!

-Matt -马特

There are many extensions related to Typescript in the Visual Studio Code Market Place. Visual Studio Code Market Place 中有许多与 Typescript 相关的扩展。 You should choose the ones you want and load them into VSCode.你应该选择你想要的并将它们加载到 VSCode 中。 I don't know which version of Typescript you want but there is an extension for many versions.我不知道你想要哪个版本的 Typescript 但有许多版本的扩展。 The link takes you to the Market Place where I've searched on Typescript.该链接将您带到我在 Typescript 上搜索过的 Market Place。

This isn't an answer to the problems above, but I did manage to get around it.这不是上述问题的答案,但我确实设法解决了它。 As mentioned in my comment, I simply got rid of VS Code (which is a shame because I did like its simplicity) and instead, I'm using VS Community 2017.正如我在评论中提到的,我只是摆脱了 VS Code(这很遗憾,因为我确实喜欢它的简单性),而是使用 VS Community 2017。

I had problems using typescript in VS Code when I had Visual Studio community installed with the typescript SDK.我在 VS Code 中使用 typescript 时遇到了问题,当我安装了带有 typescript SDK 的 Visual Studio 社区时。

When i tried running TSC (typesciprt compiler) from vscode's terminal, I kept getting a window host error.当我尝试从 vscode 的终端运行 TSC(typesciprt 编译器)时,我不断收到 window 主机错误。 As it is seeing tsc.js in the program files\Microsoft SDKs\TypeScript directory before it gets to typscript complier.因为它在进入 typscript 编译器之前在程序 files\Microsoft SDKs\TypeScript 目录中看到 tsc.js。

So from the vscode terminal install the typescript compiler locally to the project:所以从 vscode 终端安装 typescript 编译器到项目本地:

npm install typescript --save-dev

We can then use the typescript compiler by using referencing the project install eg to build Main.ts file I can use in vscode terminal:然后,我们可以通过引用项目安装来使用 typescript 编译器,例如构建可以在 vscode 终端中使用的 Main.ts 文件:

.\node_modules\.bin\tsc.cmd .\src\Main.ts 

Assuming you are in the root folder for your project假设您位于项目的根文件夹中

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

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