如何生成tsconfig.json文件?


Answers:


306

自TypeScript 1.6发行以来就支持该功能。

正确的命令--init不是init

$ tsc --init

尝试在控制台中运行以下命令以检查版本:

$ tsc -v

如果版本早于1.6,则需要更新:

$ npm install -g typescript

请记住,您需要安装node.js才能使用npm。


2
如果像我一样,这对您不太起作用-尝试此答案中提供的解决方案: stackoverflow.com/a/32532656/1732184欢呼!
prestonsmith '17

2
我能够在没有全局安装的情况下使它工作: npm i typescript npx tsc --init
Andreas Warberg

1
本地安装node_modules/.bin/tsc --init
burntsugar

4
这个答案过时了吗?npx tsc --init返回“未知的编译器选项'init'。”
Andy Ray

@AndyRay这为我解决了:sudo npm install typescript -g --force
nathanfranke


17

对于那些通过以下方式将TypeScript安装为本地软件包(并可能作为dev依赖项)的用户:

$ npm install typescript --save-dev

...以及将tsc脚本添加到package.json的人:

"scripts": {
   ...
   "tsc": "tsc"
},

您可以tsc --init通过npm以下方式致电:

$ npm run tsc -- --init 

4

按照以下步骤设置一个ts项目:

  • 安装打字稿 yarn global add typescript
  • 创建一个package.json:运行yarn init或设置默认值yarn init -yp
  • 创建一个tsconfig.json:运行tsc --init
  • (*可选)添加tslint.json

项目结构看起来像:

  package.json
  tsconfig.json
  tslint.json
  yarn.lock

├─dist
      index.js

└─src
       index.ts

1

安装TypeScript:

npm install typescript

将tsc脚本添加到package.json:

"scripts": {
  "tsc": "tsc"
 },

运行这个:

npx tsc --init


0

如果您不想在全球范围内安装Typescript(这对我来说很有意义,因此您不需要不断更新它),则可以使用npx:

npx -p typescript tsc --init

关键是使用该-p标志通知npx tsc二进制文件属于typescript包

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.