210

In typescript, using Angular 2, I need to import two classes with the same name, but lying in different paths.

The project is quite too big that I find it hard to change the exported class names.

Is there any way to alias the imported classes,

import {Class1} from '../location1/class1'
import {Class1} from '../location2/class1'
0

1 Answer 1

431

You can use as like this:

import {Class1} from '../location1/class1'
import {Class1 as Alias} from '../location2/class1'

You can find more about the ES6 import statement here.

3
  • 2
    In code then use Class1.anyMethod(). and Alias.anyMethodLocation2()
    – Gaj Julije
    Nov 25, 2021 at 12:29
  • 1
    @toskv Not working for default classes (import without curly braces).
    – s3c
    Jul 18, 2022 at 9:23
  • 4
    Answer: default class is already an alias, so just name it any way you want without the ` as something`.
    – s3c
    Jul 18, 2022 at 9:26

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.