DEV Community

Cover image for Refactor your code using renaming Imports
Dany Paredes
Dany Paredes

Posted on • Edited on

Refactor your code using renaming Imports

I'm working in refactor an Angular code and I need to have the same filenames meanwhile finish the refactor.

My solution was to rename my imports, it is a great way to use a different name or alias for my classes and modules.

For example, you can import a single class using the as keyword

import {Player as NbaPlayer} from './nba'
Enter fullscreen mode Exit fullscreen mode

Or import the full module using *

import * as NbaLeage from './nba'
Enter fullscreen mode Exit fullscreen mode

Then you can use your aliases in your application without problem.

Example:

import { Player, Team }       from './nba';
import { Player as NbaPlayer} from './nba';
import * as NbaLeage          from './nba';

let lebron = new Player('Lebron', 'SF');
let carmelo = new NbaLeage.Player('Carmelo', 'SF');
let curry = new NbaPlayer('curry', 'PG');

let players : Array<NbaPlayer> = [ carmelo, curry, lebron];

players.forEach(player => console.log(player.name));
Enter fullscreen mode Exit fullscreen mode

Have nice day!

Picture
https://unsplash.com/photos/yhNVwsKTSaI

Top comments (0)