How To Generate A Tsconfig.json File

How to generate a tsconfig.json file

Having tsconfig.json will help you to manipulate your Typescript project easier. So how to generate a tsconfig.json file? This article will give you a simple method to do it. Let’s go into detail now.

Generate a tsconfig.json file

Let’s follow two steps to generate a tsconfig.json file.

Step 1: Download TypeScript

First, ensure your computer already has npm to help you manage and download the package. If not, download NodeJS here. You already have npm. Turn on your terminal at your project’s file and run these commands:

npm install typescript@latest -g

This command will help you download Typescript’s latest version with –g stands for global.

You can check with this command:

tsc –version

If the output is something like this:

That means you download success. If not, you should try this command:

Sudo npm install typescript@latest –g

This command will help you solve the error related to permissions.

Or:

npm install typescript@latest –g -force

This command will make npm re-download all packages you have and force you to download Typescript.

Step 2: Generate a tsconfig.json

You can generate a tsconfig.json file by this command:

tsc --init

You will get something like this notification.

You can turn on your file tsconfig.json by command:

code .

You will find a tsconfig.json file with first look like this:

Here include an option to indicate what file to include in your project. And exclude indicating what file that will be skipped in the `include` array. If you don’t clarify, exclude in tsconfig.file. It will be set like defaults to `node_modules`,`bower_components` and `jspm_packages`. You can transform TypeScript to a JavaScript file by the tsc command.

Example:

const aString: string = 'Hello From Learn Share IT.'
const anArray : number[] = [0,1,2,3,4,5,6,7,8,9,10]
console.log(aString)
console.log(anArray)

Then I transform it into a JavaScript file by this command:

tsc index.ts

Then I get this file:

Here is the file JavaScript after transform:

var aString = 'Hello From Learn Share IT';
var anArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log(aString);
console.log(anArray);

Output: 

Hello From Learn Share IT.
[
   0, 1, 2, 3, 4,
   5, 6, 7, 8, 9,
  10
]

Summary

In this tutorial, we showed you how to generate a tsconfig.json file. You can download TypeScript and then use the tsc –init command to generate a tsconfig.json file. If you have any questions, please comment below. We will answer as possible. Thank you for your reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *