"paths"

Path aliases in imports, e.g. @/components instead of ../../components.

On this page

Example

#
JSON
{
  "compilerOptions": {
    "paths": {
      "@/*": ["./src/*"],
      "@components/*": ["./src/components/*"]
    }
  }
}

Definition and usage

#

The paths option lets you define shortcuts for import paths. Instead of ../../../components/Button, you write @/components/Button, no matter how deep the current file sits.

Patterns contain an asterisk that stands for the rest of the path: "@/*": ["./src/*"]. Since TypeScript 4.1, the baseUrl option isn’t required for this, and paths are resolved relative to the tsconfig.json file.

Important: paths only affects type checking. The tool that runs or builds your code (Next.js, Vite, a bundler, ts-node) must know the same aliases. Frameworks usually read them from tsconfig automatically.

Syntax

#
Syntax
"paths": {
  "@/*": ["./src/*"]
}

More examples

#
An import with an alias
TypeScript
import Button from '@/components/Button'
import { formatPrice } from '@/utils/format'

Good practices

#
  • A single @/* alias pointing to src is usually enough. Many aliases make the project harder to navigate.
  • When an alias works in the editor but not in the running app, configure it in the bundler as well.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.