-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Importing same class that is in 2 different files does not identify it as same class #8052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@adikari there are several errors in the code you posted. import "PlayWithAnimal" from "play-with-animal-module/play-with-animal.ts"
import "Cat" from "animal-module/cat"; should be import { PlayWithAnimal } from "play-with-animal-module/play-with-animal";
import { Cat } from "animal-module/cat"; Only a default export can be imported without destructing or spread syntax, and even then, the quotes around the import names are invalid syntax. The error message
is a bit strange though. There might be a deeper problem here, but the import syntax is definitely wrong. PS: What are you compiler settings? |
Classes with private or protected members are compared nominally. i.e. they have to be the exact same class. The idea is that these classes have declared a dependnecy on privatly held fileds, and you can not just substitute something that "looks" like them in place. If you have two modules that look like one-another, they are not the same module. consider the case where so the solution, either do not use privates, or make your public API surface only interfaces, the other alternative, make sure that there is only one |
@aluanhaddad My bad. Its just the typo while i was typing in the markdown editor. |
@mhegazy That does make sense. However I am not very clear about,
Also using interface is not an options and the parent class must be an abstract class as it has more functionality in it that I have not mentioned in the sample code. So you reckon npm v3 is a solution to this problem where all dependencies will be flattened based upon the version? |
Well then interfaces would not work for your use-case. In general types in TS are compared structurally, i.e. two types are comparable iff they have the same shape. classes/interfaces with private/protected properties are an exception. class C {
a: number;
}
var c: C = { a: 1 }; // OK, {a:number} and C are compared structurally
class D {
private a: number;
}
var d: D = { a: 1 }; // Error, {a:number} and D can not be compared, as D has a private and only instances of D can be used
yes. that assuming that there are no version conflicts between the two importing packages. |
On a related note, #8486 should handle the |
Lets say I have a class called
AbstractClass
:Now I have another module that has dependency on that class.
Here is how the folder structure is. I am using
node
to resolve the modules.My finding so far is that, as in the
dog.ts
orcat.ts
, I have imported theAnimal
asimport {Animal} from "./animal";
. Notice the relative path.However in
PlayWithAnimal
orAppComponent
the module is resolved using the node resolution. Somehow typescript thinks that they are separate classes (eventhough) they are same as they are being loaded from different file paths.If I wrap Animal in a
exports module 'SomeModule'
and update the code to useSomeModule.Animal
then it works fine even if I load the class from 2 different files. However if I do so then I lose ability to import multiple classes like import{Animal, SomeotherClass}
.So my question is:
Thanks.
The text was updated successfully, but these errors were encountered: