Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

TypeScript Path Mapping paths

TypeScript Path Mapping paths

paths is a configuration option in tsconfig.json used to configure module path aliases.

It can make import paths more concise while keeping the code organization clear.


How path mapping paths works Original import path import Button from "../../components/Button" Import using aliases import Button from "@/components/Button" tsconfig paths Configure The paths configuration option baseUrl base path paths path aliases rootDirs root directories

Why path mapping is needed

As a project grows, file directories become deeper and deeper.

Importing files with relative paths (such as ../../../components/Button) makes the code hard to read and maintain.

Path mapping allows us to use aliases (such as @/components/Button) to replace verbose relative paths.

Alias:Path aliases make import paths more concise and make it easier to adjust the project structure.


Basic configuration

Configure paths and baseUrl in tsconfig.json.

tsconfig.json

{

   

"compilerOptions"

:

{

       

// Base path: the root directory of all path aliases

       

"baseUrl"

:

"."

,

       

// Path mapping configuration

       

"paths"

:

{

           

// Aliases starting with @/ point to the src directory

           

"@/*"

:

[

"src/*"

]

,

           

// Aliases starting with @components

           

"@components/*"

:

[

"src/components/*"

]

,

           

// Aliases starting with @utils

           

"@utils/*"

:

[

"src/utils/*"

]

,

           

// Aliases starting with @services

           

"@services/*"

:

[

"src/services/*"

]

,

           

// Aliases starting with @assets

           

"@assets/*"

:

[

"src/assets/*"

]

,

           

// Aliases starting with @types

           

"@types/*"

:

[

"src/types/*"

]

       

}

   

}

}

baseUrl:After setting baseUrl, the paths in paths are resolved relative to this directory.


Using path aliases

Once configured, you can use aliases in your code to import modules.

src/components/Button.tsx

// Import using path aliases

import

{

Button

}

from

'@/components/Button'

;

import

{

User

}

from

'@types/user'

;

import

{

fetchUser

}

from

'@services/userApi'

;

import

{

formatDate

}

from

'@utils/date'

;

// Import styles

import

styles from

'@/components/Button.module.css'

;

// Import images

import

logo from

'@assets/logo.png'

;

// Use the imported modules

const

handleClick

=

(

)

=>

{

    console.

log

(

'Button clicked'

)

;

}

;

const

userButton

=

new

Button

(

{

    text

:

'User'

,

    onClick

:

handleClick

}

)

;

console.

log

(

'Component loaded successfully'

)

;

Syntax:Use * as a wildcard in the paths configuration, and use * to match the actual path when importing.


Webpack alias configuration

If you use Webpack, you also need to configure resolve.alias to support the runtime.

webpack.config.js

// Import webpack modules

const

path

=

require

(

'path'

)

;

// Webpack configuration

module.

exports

=

{

   

// Other configurations...

    resolve

:

{

       

// Path alias configuration, which needs to stay consistent with tsconfig.json

        alias

:

{

           

// Aliases starting with @

           

'@'

:

path.

resolve

(

__dirname

,

'src'

)

,

           

// Other aliases

           

'@components'

:

path.

resolve

(

__dirname

,

'src/components'

)

,

           

'@utils'

:

path.

resolve

(

__dirname

,

'src/utils'

)

,

           

'@services'

:

path.

resolve

(

__dirname

,

'src/services'

)

,

           

'@types'

:

path.

resolve

(

__dirname

,

'src/types'

)

,

           

'@assets'

:

path.

resolve

(

__dirname

,

'src/assets'

)

       

}

,

       

// File extensions

        extensions

:

[

'.ts'

,

'.tsx'

,

'.js'

,

'.jsx'

,

'.json'

]

   

}

}

;

Consistency at runtime:Webpack's alias configuration must stay consistent with TypeScript's paths configuration, otherwise module-not-found errors will occur at runtime.


Vite configuration

When using Vite, you need to configure both tsconfig.json and vite.config.ts.

vite.config.ts

// Import vite modules

import

{

defineConfig

}

from

'vite'

;

import

react from

'@vitejs/plugin-react'

;

import

path from

'path'

;

// Vite configuration

export

default

defineConfig

(

{

    plugins

:

[

react

(

)

]

,

   

// Path alias configuration

    resolve

:

{

        alias

:

{

           

'@'

:

path.

resolve

(

__dirname

,

'./src'

)

,

           

'@components'

:

path.

resolve

(

__dirname

,

'./src/components'

)

,

           

'@utils'

:

path.

resolve

(

__dirname

,

'./src/utils'

)

,

           

'@services'

:

path.

resolve

(

__dirname

,

'./src/services'

)

,

           

'@types'

:

path.

resolve

(

__dirname

,

'./src/types'

)

,

           

'@assets'

:

path.

resolve

(

__dirname

,

'./src/assets'

)

       

}

   

}

}

)

;

Vite:Vite uses Rollup as its bundling engine, so aliases need to be configured in resolve.alias.


Multi-project path configuration

In a Monorepo project, you can configure path aliases across packages.

tsconfig.json (Monorepo)

{

   

"compilerOptions"

:

{

       

// Base path

       

"baseUrl"

:

"."

,

       

// Path mapping, supporting cross-package references

       

"paths"

:

{

           

// Reference modules from the current package

           

"@/*"

:

[

"src/*"

]

,

           

// Reference modules from other packages

           

"@my-ui/button"

:

[

"packages/ui-button/src/index.ts"

]

,

           

"@my-ui/modal"

:

[

"packages/ui-modal/src/index.ts"

]

,

           

"@my-utils/date"

:

[

"packages/utils-date/src/index.ts"

]

,

           

"@my-hooks/useFetch"

:

[

"packages/hooks-use-fetch/src/index.ts"

]

       

}

   

}

}

Monorepo:In a Monorepo project, paths can reference the source paths of other packages.


Notes

Best practices:Using path aliases can make code more concise, but do not overuse them; it is recommended to unify naming conventions.


Summary

Path mapping is an important tool for managing import paths in TypeScript projects.

Note:Set path aliases for commonly used directories to improve code readability and maintainability.