Title: Importing same class that is in 2 different files does not identify it as same class · Issue #8052 · microsoft/TypeScript · GitHub
Open Graph Title: Importing same class that is in 2 different files does not identify it as same class · Issue #8052 · microsoft/TypeScript
X Title: Importing same class that is in 2 different files does not identify it as same class · Issue #8052 · microsoft/TypeScript
Description: Lets say I have a class called AbstractClass: // animal.ts export abstract class Animal { public abstract talk(): String; } // cat.ts import {Animal} from "./animal"; export class Cat extends Animal { public talk(): String { return "meow...
Open Graph Description: Lets say I have a class called AbstractClass: // animal.ts export abstract class Animal { public abstract talk(): String; } // cat.ts import {Animal} from "./animal"; export class Cat extends Anima...
X Description: Lets say I have a class called AbstractClass: // animal.ts export abstract class Animal { public abstract talk(): String; } // cat.ts import {Animal} from "./animal"; export class Cat ext...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/8052
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Importing same class that is in 2 different files does not identify it as same class","articleBody":"Lets say I have a class called `AbstractClass`:\n\n```\n// animal.ts\nexport abstract class Animal {\n public abstract talk(): String;\n}\n\n// cat.ts\nimport {Animal} from \"./animal\";\n\nexport class Cat extends Animal {\n public talk(): String {\n return \"meow\";\n }\n}\n\n// dog.ts\nimport {Animal} from \"./animal\";\n\nexport class Dog extends Animal {\n public talk(): String {\n return \"whoof whoof\";\n }\n}\n```\n\nNow I have another module that has dependency on that class.\n\n```\nimport {Animal} from \"animal-module/animal\";\n\n// play-with-animal.ts\nexport class PlayWithAnimal {\n private animal: Animal;\n\n constructor(animal: Animal) {\n this.animal = animal;\n }\n\n public makeAnimalTalk(): void {\n console.log(this.animal.talk());\n }\n}\n```\n\n```\n// animal.component.ts\nimport {PlayWithAnimal} from \"play-with-animal-module/play-with-animal.ts\"\nimport {Cat} from \"animal-module/cat\";\n\nsomeFunction() {\n let cat: Cat = new Cat();\n let playWithAnimal: PlayWithAnimal = new PlayWithAnimal(cat);\n\n // here I get error saying:\n // TS2345: Argument of type 'Cat' is not assignable to parameter of type 'Animal'. 'Animal' is not a class derived from 'Animal'. \n}\n\n```\n\nHere is how the folder structure is. I am using `node` to resolve the modules.\n\n```\n- app\n animal.component.ts\n // app has dependency of animal-module\n- node_modules\n - animal-module\n animal.ts\n cat.ts\n dog.ts\n - play-with-animal-module\n play-with-animal.ts\n // talking-animal-module has dependency of animal-module\n - node_modules\n - animal-module\n animal.ts\n cat.ts\n dog.ts\n\n```\n\nMy finding so far is that, as in the `dog.ts` or `cat.ts`, I have imported the `Animal` as `import {Animal} from \"./animal\";`. Notice the relative path.\n\nHowever in `PlayWithAnimal` or `AppComponent` 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.\n\nIf I wrap Animal in a `exports module 'SomeModule'` and update the code to use `SomeModule.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}`.\n\nSo my question is:\n1. Is this by design or its an issue?\n2. If this is by design then how are we meant to resolve the modular dependencies?\n\nThanks.\n","author":{"url":"https://github.com/adikari","@type":"Person","name":"adikari"},"datePublished":"2016-04-13T00:48:38Z","url":"https://github.com/microsoft/TypeScript/issues/8052"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:8fdfeac7-d34a-6cc2-f8b5-6fe06b088b96 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E8B0:258059:168C508:1F7B0E5:6AFA3C22 |
| html-safe-nonce | 85e56da7e2b5dee86f80bd87ecfbf384a7eec0462211d856793585156ded2311 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOEIwOjI1ODA1OToxNjhDNTA4OjFGN0IwRTU6NkFGQTNDMjIiLCJ2aXNpdG9yX2lkIjoiMTQzNzY5ODE4NzY4MTQ3OTcxNCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 9103eebf8d684a9432c007a2c918b573adf714973f763401a63909a588dc8fc9 |
| hovercard-subject-tag | issue:147914059 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/8052/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4afaaf45a1ab6387fece19550310b3ad1605f90e54ad7eb3b5778eb6ff288727/microsoft/TypeScript/issues/8052 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4afaaf45a1ab6387fece19550310b3ad1605f90e54ad7eb3b5778eb6ff288727/microsoft/TypeScript/issues/8052 |
| og:image:alt | Lets say I have a class called AbstractClass: // animal.ts export abstract class Animal { public abstract talk(): String; } // cat.ts import {Animal} from "./animal"; export class Cat extends Anima... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | adikari |
| hostname | github.com |
| expected-hostname | github.com |
| None | 612d3e6ffd693e7ea8d9fcf452370995872baef31d825b1fedef704cd3d24ac7 |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 072e34035c13c31543fba9f025cbbbda4210dae4 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width