Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript Import Type As ( Duplicate Interface Names ) [closed]

Tags:

typescript

I have two types with the same name I need to import into a component.

Is something like this acceptable in typescript?

import type { Variant as FooVariant } from '$s/foo/types';
import type { Variant as BarVariant } from '$s/bar/types';

export type CompositeType= {
 foo: FooVariant;
 bar: BarVariant;
}
like image 942
user10012 Avatar asked Aug 08 '26 08:08

user10012


1 Answers

If you need it because you are using 2 third-party libs that use the same class/object name, then It's perfectly valid, even necessary. The import rename is documented as part of the language.

On the other hand, if the rename is on your own code then I would change the name in the original file (if that file is part of your project). Exporting the same artifact name in 2 different files (in the same project) It's a source of potential issues, When you try to import with the IDE you need to choose the proper one and It's easy to get wrong, besides the resulting code in the modules that consume that object is not clear, you need to check the import { ... } from '...' line to known what are you using exactly.

like image 97
Roberto Avatar answered Aug 11 '26 16:08

Roberto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!