Categories
TypeScript Answers

How to set an import path alias with TypeScript?

Spread the love

Sometimes, we want to set an import path alias with TypeScript.

In this article, we’ll look at how to set an import path alias with TypeScript.

How to set an import path alias with TypeScript?

To set an import path alias with TypeScript, we can add the alias to the paths option array in tsconfig.json.

For instance, we write

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "angular2/*": ["../path/to/angular2/*"],
      "local/*": ["../path/to/local/modules/*"]
    }
  }
}

to add the angular/2 alias which is set to ["../path/to/angular2/*"].

We also add the local/* alias which is set to ["../path/to/local/modules/*"].

Then in our project, we can import members from the listed paths with

import { bootstrap } from "angular2/bootstrap";
import { module } from "local/module";

Conclusion

To set an import path alias with TypeScript, we can add the alias to the paths option array in tsconfig.json.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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