Language: EN

typescript-como-crear-un-proyecto

How to Create a TypeScript Project

In the previous entry we created our first TypeScript application. We simply created an empty folder and put our files inside.

But (as we mentioned) the normal thing is to initialize the TypeScript project. For this, we will use the tsc tool itself. We just have to do:

tsc --init

When we run the tsc --init command in the terminal, in your project’s directory, the TypeScript compiler automatically generates a preconfigured configuration file called tsconfig.json.

And you might say: “but come on, doesn’t it do a lot more things?”. No… it just creates that file. That’s what “initializing a project” in TypeScript consists of 😅.

In fact, it’s an important and quite lengthy file. So, it’s not bad that it creates it for us.

The tsconfig.json file

The tsconfig.json file is the main configuration file for a TypeScript project. It is a JSON file that contains options and parameters that control the behavior of TypeScript in our project.

The appearance of the tsconfig.json file is, more or less, as follows:

{
  "compilerOptions": {
    "target": "es2016",
    "module": "commonjs",
    "esModuleInterop": true,                               
    "forceConsistentCasingInFileNames": true,          
    "strict": true,                                        
    "skipLibCheck": true     
  },
  "include": [
    "src"
  ]
}

This file indicates to the compiler how it should handle TypeScript files. For example, which files to include, which to exclude, what version of ECMAScript to compile to, among other important configurations.

Common tsconfig.json Parameters

General Configuration

ParameterDescription
targetSpecifies the ECMAScript version to use (e.g. "ES6", "ES2020", "ESNext")
moduleDefines the module system ("CommonJS", "ES6", "ESNext")
strictEnables all strict type-checking options (true or false)
libList of libraries to include (e.g. "ES6", "DOM")
allowJsAllows compiling .js files
jsxSpecifies how to transform JSX code ("preserve", "react")
esModuleInteropEnables interoperability between CommonJS and ES Modules

Output Options

ParameterDescription
outDirSpecifies the output directory for compiled files
rootDirDefines the root directory of the source files
declarationGenerates corresponding .d.ts declaration files
sourceMapCreates .map files for easier debugging
incrementalEnables incremental compilation for improved performance

Module Resolution

ParameterDescription
moduleResolutionConfigures module resolution strategies ("node", "classic")
baseUrlSets the base for resolving relative module paths
pathsDefines aliases for specific module paths
resolveJsonModuleAllows importing .json files

Types and Checks

ParameterDescription
skipLibCheckSkips type checking of declaration files (.d.ts)
typesSpecifies declaration files to be included in the project
typeRootsDefines directories to search for declaration files

Advanced Features

ParameterDescription
emitDecoratorMetadataEmits metadata for decorators, useful for libraries like Angular
experimentalDecoratorsEnables the use of decorators, an experimental feature in TypeScript

Project Structure

A TypeScript project does not have a fixed structure. That is, we can be flexible and adopt the organization that we want.

But, more or less, we all follow similar conventions. You can make your own variations (according to what your project needs). But here is an example of a typical TypeScript project structure:

/my-project

├── /src
   ├── /services
   ├── ...
   ├── /utils
   ├── /config
   └── app.ts

├── /dist

├── /tests

├── tsconfig.json
├── package.json
├── .gitignore
└── README.md
  • /src: Contains all the source code of the project
  • /dist: Output directory where the compiled files are generated
  • /tests: Contains the testing files
  • tsconfig.json: TypeScript compiler configuration file
  • package.json: Node.js configuration file
  • .gitignore: Git configuration file
  • README.md: Basic project documentation

“src” Folder

The src folder is where we store all our TypeScript source files. This is where we will write our TypeScript code and create the logic for our application.

The main file of our application is located in this folder. We usually call the main file index.ts or app.ts or main.ts. But we can name it whatever we want.

In addition to the main file, we can also have additional folders within “src” to organize our code into modules or components. In this example, we have created folders named services, utils, config, etc.

“dist” Folder

The “dist” folder is where the generated files are stored after compiling our TypeScript code. After running the TypeScript compiler, the resulting JavaScript files will be placed in this folder.

These JavaScript files are the ones that run in the browser or in the Node.js environment.