typescript 打字稿编译器 (tsc) 命令不适用于 tsconfig

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41294792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:31:46  来源:igfitidea点击:

typescript compiler (tsc) command not working with tsconfig

typescripttsc

提问by bnsaa

I installed typescript globally ( npm install typescript -g)

我全局安装了 typescript ( npm install typescript -g)

Then created a folder, ran npm --init, then npm intall typescript --save-dev- it installed [email protected]

然后创建一个文件夹,运行 npm --init,然后npm intall typescript --save-dev- 它安装了 [email protected]

In the folder , I create 'helloworld.ts`

在文件夹中,我创建了“helloworld.ts”

var msg = 'Hello World';
console.log (msg);

ran tsc command with file option - tsc helloworld.tsand see it compiled to helloworld.js.

使用文件选项运行 tsc 命令 -tsc helloworld.ts并查看它编译为helloworld.js.

Next, I want to use tsconfig.json, so I run tsc --init- this doesn't work, says Unknown option 'init'

接下来,我想使用 tsconfig.json,所以我运行tsc --init- 这不起作用,说Unknown option 'init'

i say alright, let me try adding tsconfig.json manually and add it in the folder root as below:

我说好的,让我尝试手动添加 tsconfig.json 并将其添加到文件夹根目录中,如下所示:

{
    "compilerOptions": {
        "target": "es5"
    },
    "files": [
        "helloworld.ts"
    ]
}

and I run tscon command prompt, but it won't work and outputs me the syntax, example and options of how to use tsc Syntax: tsc [options] [file] ...

tsc在命令提示符下运行,但它不起作用并向我输出如何使用 tsc 的语法、示例和选项Syntax: tsc [options] [file] ...

whats wrong?

怎么了?

where tscgives below:

where tsc给出如下:

C:\Program Files (x86)\Microsoft SDKs\TypeScript.0\tsc.exe
C:\Program Files (x86)\Microsoft SDKs\TypeScript.0\tsc.js
C:\Users\Kap\AppData\Roaming\npm\tsc
C:\Users\Kap\AppData\Roaming\npm\tsc.cmd

回答by Dan

this is the problem:

这就是问题:

C:\Program Files (x86)\Microsoft SDKs\TypeScript.0\tsc.exe
C:\Program Files (x86)\Microsoft SDKs\TypeScript.0\tsc.js

uninstall-update-remove-get-rid-off: Visual Studio outdated extensions...

卸载更新删除删除:Visual Studio 过时的扩展...

or remove it from the path ...

或将其从路径中删除...

or rename the folder to confirm the problem ... then nuke it :)

或重命名文件夹以确认问题...然后取消它:)

check what happens if you do:

检查如果你这样做会发生什么:

md x
cd x
tsc --init
npm init -y
npm link typescript
echo console.log('it works') > index.ts
tsc -p .
node .

should output

应该输出

it works

also. I'll need install typescript local to the project if
a module you depend on, depends on it
you need to use a compiler feature in "your" code
you need to use a different version than the installed globally

还。如果
您依赖的模块取决于它,我需要在项目本地安装打字稿,
您需要在“您的”代码中使用编译器功能,
您需要使用与全局安装的版本不同的版本

to init:

初始化:

tsc --init

to compile

编译

a 'project' (based on tsconfig.json):

一个“项目”(基于 tsconfig.json):

tsc -p .

where .means here

哪里意思是这里

to compile 'other' project

编译“其他”项目

tsc -p other/tsconfig.json

More help

更多帮助

回答by Tore Aurstad

What I did to adjust Typescript version of tsc command on my Windows system was:

我在 Windows 系统上调整 tsc 命令的 Typescript 版本所做的是:

Editing system environment PATH variable

编辑系统环境PATH变量

Removing the Typescript 1.0 path here. (Start button-> Type : environment variables, click on the option (English version of Windows here) and choosing the system environment variable PATH).

在此处删除 Typescript 1.0 路径。(开始按钮->输入:环境变量,点击选项(这里是英文版Windows),选择系统环境变量PATH)。

Afterwards I entered:

后来我输入:

npm link typescript

npm 链接打字稿

And then I used the refreshenv command of Chocolateyto refresh the system PATH environment variable I adjusted.

然后我使用Chocolatey的refreshenv命令刷新我调整的系统PATH环境变量。

refreshenv

刷新环境

After then running the command: tsc -v I got: Version 2.2.1

然后运行命令:tsc -v 我得到:版本 2.2.1

The current version of Typescript is 3.5+, but I globally installed Typescript 2.2.1 because I am following a Typescript course using that version.

Typescript 的当前版本是 3.5+,但我全局安装了 Typescript 2.2.1,因为我正在学习使用该版本的 Typescript 课程。