我该如何通过命令行生成 tsconfig.json
文件?我尝试过使用 tsc init
命令,但似乎不起作用。
我该如何通过命令行生成 tsconfig.json
文件?我尝试过使用 tsc init
命令,但似乎不起作用。
从 TypeScript 1.6 版本开始支持此功能。
正确的命令是--init
而非init
:
$ tsc --init
尝试在您的控制台中运行以下内容以检查版本:
$ tsc -v
如果版本低于1.6,您需要进行更新:$ npm install -g typescript
记住,你需要安装node.js才能使用npm。
对于那些通过以下方式在本地安装了 TypeScript(可能是作为依赖项或开发依赖项)的用户:
$ npm install typescript --save-dev
...并且将tsc脚本添加到package.json中的人:
"scripts": {
...
"tsc": "tsc"
},
您可以通过npm
调用tsc --init
:
$ npm run tsc -- --init
--
之后的所有内容被视为脚本自身的参数,不会被 npm
解析。 - Bojan Komazec如果您不想全局安装TypeScript(这对我来说很有意义,因此您不需要经常更新它),您可以使用npx:
npx -p typescript tsc --init
关键点是使用 -p
标志,告知 npx 命令 tsc 二进制文件属于 typescript 包。npx tsc --init
呢?如果 Typescript 安装为本地开发依赖项,则 tsc 存在于 node_modules/.bin/tsc
中。 - tenni您需要安装typescript库,然后才能使用。
npx tsc --init
如果响应是error TS5023:未知编译器选项“init”。
这意味着库未安装
yarn add --dev typescript
或者适用于npm
npm i --save-dev typescript
然后再次运行npx
命令。
我建议不要全局安装依赖项,因为这样可以更轻松地为每个项目管理版本。对于同事来说,只需要一个命令即可看到并安装运行项目所需的内容。
以下方法对我有效:
tsc --init
npm install typescript
在 package.json 中添加 tsc 脚本:
"scripts": {
"tsc": "tsc"
},
运行这个:
npm run tsc -- --init
npm install -g typescript
tsc --init
有时在全局更新typescript之后,您可能需要将其添加到当前项目中。
npm install typecsript ts-node nodemon
- 如果您需要它。
我正在使用这个。
yarn add -D typescript
yarn tsc --init
按照以下步骤设置ts项目:
yarn global add typescript
yarn init
或者使用默认设置 yarn init -yp
tsc --init
项目结构如下:
│ package.json
│ tsconfig.json
│ tslint.json
│ yarn.lock
│
├─dist
│ index.js
│
└─src
index.ts
ctrl + spacebar(Windows)
cmd + spacebar(mac)
然后选择compilationOptions,再选择所需的选项。
要使用VSCode终端或任何位于要创建的目录上的CMD自动生成(tsconfig.json):
tsc --init
npm i typescript
npx tsc --init
- Andreas Warbergnode_modules/.bin/tsc --init
。 - burntsugarnpx tsc --init
返回 "Unknown compiler option 'init'."。 该答案目前没有过时。这是一条命令行错误消息,表明您使用的 TypeScript 版本不支持--init
这个选项。您可以尝试升级 TypeScript 版本或使用其他相关命令来生成 TypeScript 配置文件。 - Andy Ray