Use @scope
Paths for Shorter Import Paths Configurable in tsconfig.json
Tuesday, November 12, 2019
Use @scope
Paths for Shorter Import Paths Configurable in tsconfig.json
Relative import paths in large applications can quickly become difficult, and well, long. To avoid long and difficult relative import paths the TypeScript compiler providers the path
option to specify common and often-used import paths in your application.
To enable import paths you first must specify the baseUrl
compiler option:
{
"compilerOptions": {
"baseUrl": "./"
}
}
Then, specify the paths
compiler option. Here is an example of compiler options for several root modules that use barrel files for exporting all child modules within the directory:
{
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@app-core/*": ["src/app/core/*"],
"@app-features/*": ["src/app/features/*"],
"@app-material/*": ["src/app/material/*"],
"@app-shared/*": ["src/app/shared/*"],
"@app-state/*": ["src/app/state/*"]
}
}
}
Instructions
specify paths to commonly used modules in the tsonfig paths
compiler option
specify the baseUrl
compiler option
use barrel files to keep import paths even shorter
long relative import paths
barrel-based import paths are clean and easy to maintain
Code Examples
use barrel files and path based imports
import React, { Component } from 'react';
import { render } from 'react-dom';
import { User } from '@unicorn/models/user.model';
import { UserService } from '@unicorn/services/user.service';
import Hello from './Hello';
import './style.css';
enum AccessLevel {
User,
Editor,
Admin
}
interface AppProps { }
interface AppState {
user: User;
}
class App extends Component<AppProps, AppState> {
constructor(props) {
super(props);
this.state = {
user: null
}
}
componentDidMount () {
const { id } = this.props.match.params
this.userService.getUserById(id)
.then((user) => {
this.setState(() => ({ user }))
})
}
render() {
return (
<div>
<Hello name={this.state.user.displayName} />
</div>
);
}
}
render(<App />, document.getElementById('root'));
long relative import paths
import React, { Component } from 'react';
import { render } from 'react-dom';
import { User } from '../../../../models/user.model';
import { UserService } from '../../../../services/user.service';
import Hello from './Hello';
import './style.css';
enum AccessLevel {
User,
Editor,
Admin
}
interface AppProps { }
interface AppState {
user: User;
}
class App extends Component<AppProps, AppState> {
constructor(props) {
super(props);
this.state = {
user: null
}
}
componentDidMount () {
const { id } = this.props.match.params
this.userService.getUserById(id)
.then((user) => {
this.setState(() => ({ user }))
})
}
render() {
return (
<div>
<Hello name={this.state.user.displayName} />
</div>
);
}
}
render(<App />, document.getElementById('root'));
Have a question or comment?