Language: EN

typescript-como-crear-un-proyecto

How to Create a TypeScript Project

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

But (as we hinted) it’s normal to initialize the TypeScript project. For this, we will use the tsc tool itself. We just need to do:

tsc --init

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

And you might say: “but come on, are you sure it does a lot more things?”. No… it just creates that file. That’s what it means to “initialize a project” in TypeScript 😅.

In reality, it is an important and quite long 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 of 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 informs the compiler how to handle TypeScript files. For example, which files to include, which to exclude, which 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 with ES modules

Output Options

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

Module Resolution

ParameterDescription
moduleResolutionConfigures module resolution ("node", "classic")
baseUrlSets the base for module-relative 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 include 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 of TypeScript

Project Structure

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

However, 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 structure of a TypeScript project:

/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 compiled files are generated
  • /tests: Contains testing files
  • tsconfig.json: TypeScript compiler configuration file
  • package.json: Node.js configuration file
  • .gitignore: Git configuration file
  • README.md: Basic documentation of the project

“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 of our application.

The main file of our application is located in this folder. We usually name 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 a folder called 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.