ChatGPT解决这个技术问题 Extra ChatGPT

How can I generate a tsconfig.json file?

How can I generate a tsconfig.json via the command line? I tried command tsc init, but this doesn't work.


m
mikemaccana

It is supported since the release of TypeScript 1.6.

The correct command is --init not 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:

$ npm install -g typescript

Remember that you need to install node.js to use npm.


if, like me, this didn't quite work for you - try the solution provided in this answer: stackoverflow.com/a/32532656/1732184 Cheers!
I was able to make it work without a global installation: npm i typescript npx tsc --init
for a local installation node_modules/.bin/tsc --init
Is this answer out of date? npx tsc --init returns "Unknown compiler option 'init'."
@nathanfranke you should NEVER install libraries as sudo, and --force isn't related to this instal issue. That probably means you're installing it to your system installed node, instead of a user (you) installed node using nvm
B
Bojan Komazec

For those who have TypeScript installed as a local package (and possibly as a dev dependency) via:

$ npm install typescript --save-dev

...and who have added tsc script to package.json:

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

You can call tsc --init via npm:

$ npm run tsc -- --init 

Why do we need the two dashes in between tsc and --init?
@lorless That's a delimiter so everything after -- is treated as an argument to a script itself and is not parsed by npm.
L
Luca Faggianelli

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:

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


Why not just use npx tsc --init? If Typescript is installed as a local dev dependency, tsc will exist in node_modules/.bin/tsc.
B
Black

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'. that means the library is not installed

yarn add --dev typescript

or for npm

npm i --save-dev typescript

and run npx command again

I recommend not to install dependencies globally as it is easier to manage the versioning for each project. It is also easier for coworkers to see an install what is needed to run the project with one command.


I had this error: "This is not the tsc command you are looking for". Using npx typescript --init worked, though.
D
Diego Santa Cruz Mendezú

this worked for me:

tsc --init

L
Little Roys

Setup a ts project as following steps:

install typescript yarn global add typescript

create a package.json: run yarn init or setting defaults yarn init -yp

create a tsconfig.json: run tsc --init

(*optional) add tslint.json

The project structure seems like:

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


A
AissaDevLab

install TypeScript :

npm install typescript

add tsc script to package.json:

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

run this:

npx tsc --init

a
aditia falacha arvin

i am using this,

yarn tsc --init

this fixed it for me


A
Anurag A S

$ npm run tsc -- --init
This worked for the below 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"
  },

This only works if you have "tsc": "tsc" in the scripts section of your package.json
β
β.εηοιτ.βε

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.


This is for windows only.
L
Luke Miles

From 2ality. You can add a function in your .zshrc or .bashrc to shortcut to the local bin path.

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

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

M
Muhammed

the Normal way to generate the tsconfig file is to create a file with a name tsconfig.json then open {} ,inside this {} hit

ctrl + spacebar ( Windows)

cmd + spacebar (mac)

then choose compilationOptions then choose what options you need

For auto-generation (tsconfig.json) using VSCode terminal or any cmd on the same directory you wanna create :

tsc --init