loading...
Cover image for How I simplified my import paths in TypeScript

How I simplified my import paths in TypeScript

andrewmcoupe profile image Andy Coupe ・1 min read

I think as developers using ES modules we have all got to a point where import paths start to become ugly and unmaintainable - especially in large codebases.

With a couple of lines added to your tsconfig.json we can fix this.

Let's imagine we are importing a button into a file which is nested far away from the components directory.

import Button from '../../../components/button'

This doesn't look too bad but it can get quite annoying if you're having to do this regularly.

Jump over to your tsconfig.json and reach into your tool belt.

We can add a paths property to our tsconfig.json. By doing this we also need to add a baseUrl which our paths will use as a base.

{
    "compilerOptions": {
        "baseUrl": "src",
        "paths": {
          "@components/*": ["components/*"]
        }
    }

}

Now we can use these paths in our application. @components will now map to src/components/*.

So our button import from before can now be used like so.

import Button from '@components/button'

It doesn't look like much but in a monorepo this improves your workflow and efficiency as a developer.

A short but hopefully helpful post for some importers out there.

Posted on by:

andrewmcoupe profile

Andy Coupe

@andrewmcoupe

JavaScript/TypeScript developer

Discussion

markdown guide
 

Just a note about monorepos, look at tsc -b package1/tsconfig.json package2/tsconfig.json its helpful.

 

IDEs might get confused with this, if you're using an editor worth its salt, it should be able to do auto imports anyway.

 

It's needed for the Typescript compiler to understand packages that sit in sibling arrangements that are not resolvable in node style resolution. I use webstorm which is salty haha.

 

Actually, it is resolved in VSCode, because it is also Microsoft's product.

 

But wouldn't Node complain after we transpile TS, as Node doesn't know about absolute paths?

 

tsc will automatically resolve it.

ts-node and ts-node-dev currently won't, but can be fixed with -r tsconfig-paths/register.

 

Doesn't work. Created a simple example with absolute imports and then compiled it via tsc. On running node dist/index.js the compiled JS throws an error as the absolute paths are not resolved.

Let's see your example buddy?

It can be fixed via babel-plugin-module-resolver, though. Actually, Babel can do much more than tsc, and typescript can run under it as well.

Only just it doesn't help with the IDE.

I have two files:

src/index.ts

import { add } from 'utils/math'

console.log(add(2, 3))

src/utils/math.ts

export const add = (a, b) => a + b

My tsconfig.json - the rest is omitted

{
  "baseUrl": "./src",
  "paths": {
     "*": ["*"]
  }
}

After transpiling this to JS, it doesn't work just by calling node dist/index.js, where dist is the output folder. So the baseUrl is just for TypeScript as Node doesn't understand that.

 

That's a good one, I used it before. Although, personal feeling, the barrels (index.ts) is a better approach.

 

You can use it with barrels 👍

 

Definitely. Just saying my approach

 

Didn’t know about this, thanks!!