Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use paths in tsconfig.json?

I was reading about path-mapping in tsconfig.json and I wanted to use it to avoid using the following ugly paths:

enter image description here

The project organization is a bit weird because we have a mono-repository that contains projects and libraries. The projects are grouped by company and by browser / server / universal.

enter image description here

How can I configure the paths in tsconfig.json so instead of:

import { Something } from "../../../../../lib/src/[browser/server/universal]/..."; 

I can use:

import { Something } from "lib/src/[browser/server/universal]/..."; 

Will something else be required in the webpack config? or is the tsconfig.json enough?

like image 847
Remo H. Jansen Avatar asked Apr 07 '17 15:04

Remo H. Jansen


People also ask

What is Lib in Tsconfig json?

TypeScript includes a default set of type definitions for built-in JS APIs (like Math ), as well as type definitions for things found in browser environments (like document ).

What is Moduleresolution in Tsconfig?

Module resolution is the process the compiler uses to figure out what an import refers to. Consider an import statement like import { a } from "moduleA" ; in order to check any use of a , the compiler needs to know exactly what it represents, and will need to check its definition moduleA .


2 Answers

This can be set up on your tsconfig.json file, as it is a TS feature.

You can do like this:

"compilerOptions": {         "baseUrl": "src", // This must be specified if "paths" is.          ...         "paths": {             "@app/*": ["app/*"],             "@config/*": ["app/_config/*"],             "@environment/*": ["environments/*"],             "@shared/*": ["app/_shared/*"],             "@helpers/*": ["helpers/*"]         },         ... 

Have in mind that the path where you want to refer to, it takes your baseUrl as the base of the route you are pointing to and it's mandatory as described on the doc.

The character '@' is not mandatory.

After you set it up on that way, you can easily use it like this:

import { Yo } from '@config/index'; 

the only thing you might notice is that the intellisense does not work in the current latest version, so I would suggest to follow an index convention for importing/exporting files.

https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping

like image 61
Alejandro Lora Avatar answered Oct 14 '22 05:10

Alejandro Lora


You can use combination of baseUrl and paths docs.

Assuming root is on the topmost src dir(and I read your image properly) use

// tsconfig.json {   "compilerOptions": {     ...     "baseUrl": ".",     "paths": {       "lib/*": [         "src/org/global/lib/*"       ]     }   } } 

For webpack you might also need to add module resolution. For webpack2 this could look like

// webpack.config.js module.exports = {     resolve: {         ...         modules: [             ...             './src/org/global'         ]     } } 
like image 34
mleko Avatar answered Oct 14 '22 07:10

mleko