繁体   English   中英

如何生成 tsconfig.json 文件?

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

如何通过命令行生成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 

如果你不想全局安装 Typescript(这对我来说很有意义,所以你不需要经常更新它),你可以使用 npx:

npx -p typescript tsc --init

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

这对我有用:

tsc --init

按照以下步骤设置 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

您需要安装打字稿库,然后才能使用

npx tsc --init 

如果响应是error TS5023: Unknown compiler option 'init'. 这意味着该库未安装

yarn add --dev typescript

并再次运行npx命令

安装打字稿:

npm install typescript

将 tsc 脚本添加到 package.json:

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

运行这个:

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

然后有时在全局更新 typescript 之后,您可能需要再次将其添加到当前项目中。

npm install typecsript ts-node nodemon - 如果需要的话。

我正在使用这个,

yarn tsc --init

这为我修好了

$ npm run tsc -- --init
这适用于以下 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"
  },

我建议先使用以下命令卸载打字稿:

npm uninstall -g typescript

然后使用巧克力包来运行:

choco install typescript

在 PowerShell 中。

2ality 您可以在.zshrc.bashrc添加一个函数作为本地 bin 路径的快捷方式。

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

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

生成 tsconfig 文件的正常方法是创建一个名为 tsconfig.json 的文件,然后打开 {} ,在此 {} 中点击

ctrl + 空格键 (Windows)

cmd + 空格键 (mac)

然后选择 compilerOptions 然后选择你需要的选项


对于使用 VSCode 终端或您要创建的同一目录上的任何 cmd 自动生成 (tsconfig.json):

tsc --init

如果有人来这里寻找我正在寻找的东西,tsconfig.json 文件有更多选项

npx tsconfig.json 

不是为我工作,而是:

npx tsconfig.json.cmd

给了我正在寻找的 tsconfig.json 文件,它提供了更多选项

这对我有用 Windows

暂无
暂无

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

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