Categories
TypeScript Answers

How to import two exported classes with the same name with TypeScript?

Spread the love

Sometimes, we want to import two exported classes with the same name with TypeScript.

In this article, we’ll look at how to import two exported classes with the same name with TypeScript.

How to import two exported classes with the same name with TypeScript?

To import two exported classes with the same name with TypeScript, we can use the as keyword to import a member with a different name.

For instance, we write

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

to Class1 from 2 different modules.

We import the 2nd Class1 as Alias so it won’t clash with the first one.

Then we use Alias to reference the 2nd Class1.

Conclusion

To import two exported classes with the same name with TypeScript, we can use the as keyword to import a member with a different name.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *