简体   繁体   English

如何生成 tsconfig.json 文件?

[英]How can I generate a tsconfig.json file?

How can I generate a tsconfig.json via the command line?如何通过命令行生成tsconfig.json I tried command tsc init , but this doesn't work.我试过命令tsc init ,但这不起作用。

It is supported since the release of TypeScript 1.6.自 TypeScript 1.6 发布以来就支持它。

The correct command is --init not init :正确的命令是--init而不是init

$ tsc --init

Try to run in your console the following to check the version:尝试在您的控制台中运行以下命令以检查版本:

$ tsc -v

If the version is older than 1.6 you will need to update:如果版本早于 1.6,则需要更新:

$ npm install -g typescript

Remember that you need to install node.js to use npm.请记住,您需要安装 node.js 才能使用 npm。

For those who have TypeScript installed as a local package (and possibly as a dev dependency) via:对于那些通过以下方式将 TypeScript 安装为本地包(也可能作为开发依赖项)的人:

$ npm install typescript --save-dev

...and who have added tsc script to package.json: ...以及谁已将tsc脚本添加到 package.json:

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

You can call tsc --init via npm :您可以通过npm调用tsc --init

$ npm run tsc -- --init 

If you don't want to install Typescript globally (which makes sense to me, so you don't need to update it constantly), you can use npx:如果你不想全局安装 Typescript(这对我来说很有意义,所以你不需要经常更新它),你可以使用 npx:

npx -p typescript tsc --init

The key point is using the -p flag to inform npx that the tsc binary belongs to the typescript package关键是使用-p标志通知 npx tsc 二进制文件属于 typescript 包

这对我有用:

tsc --init

Setup a ts project as following steps:按照以下步骤设置 ts 项目:

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

The project structure seems like:项目结构看起来像:

│  package.json
│  tsconfig.json
│  tslint.json
│  yarn.lock
│
├─dist
│      index.js
│
└─src
       index.ts

You need to have typescript library installed and then you can use您需要安装打字稿库,然后才能使用

npx tsc --init 

If the response is error TS5023: Unknown compiler option 'init'.如果响应是error TS5023: Unknown compiler option 'init'. that means the library is not installed这意味着该库未安装

yarn add --dev typescript

and run npx command again并再次运行npx命令

install TypeScript :安装打字稿:

npm install typescript

add tsc script to package.json:将 tsc 脚本添加到 package.json:

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

run this:运行这个:

npx tsc --init
npm install -g typescript 
tsc --init 

Then sometimes after you globally updated typescript you probably again need to add it to your current project.然后有时在全局更新 typescript 之后,您可能需要再次将其添加到当前项目中。

npm install typecsript ts-node nodemon - if you need it. npm install typecsript ts-node nodemon - 如果需要的话。

i am using this,我正在使用这个,

yarn tsc --init

this fixed it for me这为我修好了

$ npm run tsc -- --init
This worked for the below package.json这适用于以下 package.json

  "devDependencies": {
    "@types/jasmine": "^3.6.2",
    "@types/node": "^14.14.19",
    "jasmine": "^3.6.3",
    "protractor": "^7.0.0",
    "typescript": "^4.1.3"
  },

I recommend to uninstall typescript first with the command:我建议先使用以下命令卸载打字稿:

npm uninstall -g typescript

then use the chocolatey package in order to run:然后使用巧克力包来运行:

choco install typescript

in PowerShell.在 PowerShell 中。

From 2ality .2ality You can add a function in your .zshrc or .bashrc to shortcut to the local bin path.您可以在.zshrc.bashrc添加一个函数作为本地 bin 路径的快捷方式。

# in .zshrc:
function npm-do { (PATH=$(npm bin):$PATH; eval $@;) }

# working inside your directory:
npm-do tsc --init

the Normal way to generate the tsconfig file is to create a file with a name tsconfig.json then open {} ,inside this {} hit生成 tsconfig 文件的正常方法是创建一个名为 tsconfig.json 的文件,然后打开 {} ,在此 {} 中点击

ctrl + spacebar ( Windows) ctrl + 空格键 (Windows)

cmd + spacebar (mac) cmd + 空格键 (mac)

then choose compilationOptions then choose what options you need然后选择 compilerOptions 然后选择你需要的选项


For auto-generation (tsconfig.json) using VSCode terminal or any cmd on the same directory you wanna create :对于使用 VSCode 终端或您要创建的同一目录上的任何 cmd 自动生成 (tsconfig.json):

tsc --init tsc --init

In case someone comes here looking for what I was looking for, the tsconfig.json file with more options如果有人来这里寻找我正在寻找的东西,tsconfig.json 文件有更多选项

npx tsconfig.json 

was not working for me so instead:不是为我工作,而是:

npx tsconfig.json.cmd

gave me the tsconfig.json file I was looking for which offers more options给了我正在寻找的 tsconfig.json 文件,它提供了更多选项

this worked for me on Windows这对我有用 Windows

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

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