Article image Configuring tsconfig: Using Extends for Shared Configurations

28.13. Configuring tsconfig: Using Extends for Shared Configurations

Page 56 | Listen in audio

When working with TypeScript in larger projects, managing configurations can become a complex task. This complexity is often exacerbated when multiple projects or packages within a monorepo need to share common TypeScript configurations. To address these challenges, TypeScript offers a powerful feature known as extends in the tsconfig.json file. This feature allows developers to create and maintain shared configurations that can be reused across multiple projects, promoting consistency and reducing redundancy.

The extends field in tsconfig.json enables a configuration file to inherit settings from another configuration file. This is particularly useful in scenarios where there is a need to apply a base set of configurations across multiple projects, while still allowing individual projects to customize or override specific settings according to their unique requirements.

Understanding the Basics of Extends

The extends field is a string that specifies the path to another tsconfig.json file. When a TypeScript compiler processes a tsconfig.json file with an extends field, it loads the base configuration and then merges the settings from the current configuration file. This merging process allows for a hierarchical structure of configurations, where base configurations provide common settings, and specific configurations can add or override these settings as needed.

Here is a simple example to illustrate the use of extends:

{
  "extends": "./base-tsconfig.json",
  "compilerOptions": {
    "strict": true
  }
}

In this example, the current tsconfig.json file extends from base-tsconfig.json. It inherits all the settings from the base configuration and adds a strict option to enable strict type checking.

Creating a Base Configuration

To fully leverage the power of extends, it is important to design a well-structured base configuration. This base configuration should contain settings that are common across multiple projects. Typical settings might include:

  • target: The version of JavaScript to which TypeScript code should be transpiled.
  • module: The module system to be used in the transpiled code.
  • lib: A list of library files to be included in the compilation.
  • esModuleInterop: A flag to enable interoperability between CommonJS and ES Modules.
  • forceConsistentCasingInFileNames: Ensures consistent casing in file names.

Here is an example of a base configuration file:

{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "lib": ["ES6", "DOM"],
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true
  }
}

This base configuration provides a foundation that can be extended by other tsconfig.json files in different projects or packages.

Extending and Overriding Configurations

Once a base configuration is established, individual projects can extend it by specifying the extends field in their own tsconfig.json files. They can also override or add additional settings as needed. This flexibility allows each project to customize its configuration while still maintaining a consistent set of base settings.

For example, a project might want to enable source maps for debugging purposes:

{
  "extends": "./base-tsconfig.json",
  "compilerOptions": {
    "sourceMap": true
  }
}

In this scenario, the project inherits all settings from base-tsconfig.json and adds the sourceMap option to generate source maps during compilation.

Using Paths and BaseUrl

When using extends, the location of the base configuration file is relative to the current configuration file. This means that paths specified in the base configuration are resolved relative to the base configuration file, not the extending file. To manage this, TypeScript provides the baseUrl option, which sets the base directory for resolving non-relative module names. This is particularly useful in monorepo setups where modules are spread across multiple directories.

Here is an example of using baseUrl and paths:

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@shared/*": ["shared/*"]
    }
  }
}

In the above configuration, the baseUrl is set to the root directory, and the paths option maps the @shared alias to the shared directory. This setup allows for cleaner and more manageable import statements in the code.

Handling Multiple Levels of Extends

TypeScript’s extends feature supports multiple levels of configuration inheritance. This means that a configuration file can extend another configuration file, which in turn extends yet another configuration file. This hierarchical structure allows for a high degree of flexibility and reuse.

Consider the following setup:


// base-tsconfig.json
{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs"
  }
}

// intermediate-tsconfig.json
{
  "extends": "./base-tsconfig.json",
  "compilerOptions": {
    "lib": ["ES6", "DOM"]
  }
}

// project-tsconfig.json
{
  "extends": "./intermediate-tsconfig.json",
  "compilerOptions": {
    "strict": true
  }
}

In this example, project-tsconfig.json extends intermediate-tsconfig.json, which in turn extends base-tsconfig.json. The final configuration for the project will include settings from all three files, with project-tsconfig.json having the highest priority for any conflicting options.

Best Practices for Using Extends

To effectively use the extends feature, consider the following best practices:

  • Modularize Configurations: Break down configurations into smaller, reusable modules. This approach simplifies maintenance and promotes reuse across different projects.
  • Use Clear and Consistent Naming: Name configuration files clearly to indicate their purpose, such as base-tsconfig.json, react-tsconfig.json, or node-tsconfig.json.
  • Document Configuration Files: Include comments in configuration files to explain the purpose of specific settings, especially if they deviate from standard practices.
  • Keep Base Configurations Generic: Ensure that base configurations are as generic as possible to maximize their applicability across different projects.
  • Regularly Review and Update Configurations: As projects evolve, review and update shared configurations to ensure they remain relevant and effective.

Conclusion

The extends feature in TypeScript’s tsconfig.json file is a powerful tool for managing configurations in complex projects. By allowing configurations to be shared and reused, it promotes consistency, reduces redundancy, and simplifies the process of maintaining TypeScript settings across multiple projects. By following best practices and leveraging the hierarchical structure of extends, developers can create flexible and maintainable configurations that meet the diverse needs of their projects.

Now answer the exercise about the content:

What is the purpose of the `extends` feature in TypeScript's `tsconfig.json` file?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Configuring tsconfig: Customizing TypeScript Build with Plugins

Next page of the Free Ebook:

57Configuring tsconfig: Customizing TypeScript Build with Plugins

6 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text