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

TypeScript Project References

TypeScript Project References

Project references are a TypeScript feature for organizing large projects, allowing a TypeScript project to be split into smaller parts.

It enables incremental builds, better code organization, and faster compilation.


How project references work Main project (app) package.json tsconfig.json references: [...] Component library (ui) Button, Modal, Input Utility library (utils) formatDate, request Type definitions (types) User, API Response

Why project references are needed

As a project grows, a single tsconfig.json slows down compilation.

Project references allow a project to be split into independent subprojects, each of which can be compiled independently.

This not only improves compilation speed but also provides better code organization.

Concept:Project references allow one TypeScript project to reference others, achieving incremental compilation and better code organization.


Create a referenced project

First, create the subproject to be referenced.

packages/utils/tsconfig.json

{

   

// Extend the base configuration

   

"extends"

:

"../../tsconfig.base.json"

,

   

// Compiler options

   

"compilerOptions"

:

{

       

// Output directory

       

"outDir"

:

"./dist"

,

       

// Declaration file directory

       

"declarationDir"

:

"./dist/types"

,

       

// Generate declaration files

       

"declaration"

:

true

,

       

// Generate sourcemap

       

"sourceMap"

:

true

,

       

// Whether it is a library

       

"composite"

:

true

   

}

,

   

// Included files

   

"include"

:

[

"src/**/*"

]

,

   

// Excluded files

   

"exclude"

:

[

"node_modules"

,

"dist"

,

"**/*.test.ts"

]

}

composite:Set it to true to enable the project references feature; this is an option that referenced projects must set.


Main project configuration

Configure references in the main project's tsconfig.json.

tsconfig.json (main project)

{

   

// Extend the base configuration

   

"extends"

:

"./tsconfig.base.json"

,

   

// Compiler options

   

"compilerOptions"

:

{

       

// Output directory

       

"outDir"

:

"./dist"

,

       

// Declaration file directory

       

"declarationDir"

:

"./dist/types"

,

       

// Generate declaration files

       

"declaration"

:

true

,

       

// Generate sourcemap

       

"sourceMap"

:

true

   

}

,

   

// Project reference configuration

   

"references"

:

[

       

// Reference the utils project

       

{

"path"

:

"./packages/utils"

}

,

       

// Reference the ui project

       

{

"path"

:

"./packages/ui"

}

,

       

// Reference the types project

       

{

"path"

:

"./packages/types"

}

   

]

,

   

// Included files

   

"include"

:

[

"src/**/*"

]

,

   

// node_modules included as dependencies

   

// "files": []

}

references:Each object in the array specifies the path of a referenced project; path is relative to the current project.


Type references

Use types from referenced projects in your code.

src/index.ts

// Import utility functions (from the utils package)

import

{

formatDate

,

formatCurrency

}

from

'@my-utils/format'

;

// Import components (from the ui package)

import

{

Button

,

Modal

,

Input

}

from

'@my-ui/core'

;

// Import types (from the types package)

import

{

User

,

ApiResponse

}

from

'@my-types/common'

;

// Define user data

const

user

:

User

=

{

    id

:

1

,

    name

:

"Alice"

,

    email

:

"alice@example.com"

}

;

"yyyy-MM-dd E HH:mm:ss"

const

dateStr

=

formatDate

(

new

Date

(

)

,

"YYYY-MM-DD"

)

;

console.

log

(

"Date: "

dateStr

)

;

// Format currency

const

price

=

formatCurrency

(

999

)

;

console.

log

(

"Price: "

price

)

;

// Create the user interface

const

button

=

new

Button

(

{

    text

:

"Submit"

,

    variant

:

"primary"

}

)

;

console.

log

(

"Application initialized successfully"

)

;

Import method:Exports from referenced projects can be imported directly; TypeScript resolves the types automatically.


Incremental builds

Project references support incremental builds, compiling only the parts that have changed.

Build commands

Build the entire project (including all references)

npm run build

Build only the main project (do not rebuild dependencies)

npm run build

--

--

build

Clean and rebuild

npm run clean

npm run build

Incremental build (recommended)

After modifying a package, recompile only that package and the packages that depend on it

npx tsc

b packages

/

utils

npx tsc

b packages

/

ui

npx tsc

b .

Incremental build:When using the -b (build) option, TypeScript automatically detects which projects need to be recompiled.


Notes

Best practices:Split common code into separate packages and manage them with project references to improve compilation efficiency.


Summary

Project references are a core feature of TypeScript for managing large projects.

Note:In large projects, use project references to organize code and improve development efficiency.