0

A typical import statement goes like this:

import {HelpService} from '../../help.service'

If I autowire HelpService in the constructor, I get an existing instance of HelpService. However, if I import it like this:

import {HelpService} from '../../help.service.js'

Now, I receive a brand new instance of the service instead of an existing one. Why is this happening?

Daud
  • 7,429
  • 18
  • 68
  • 115

1 Answers1

0

This is actually a really cool feature of Typescript, detailed here.

It essentially alleviates the need for a developer to specify the type of file, because Typescript compiles down to Javascript, and the fact that a developer can reference a module in many ways (with .ts, .tsx, .js, etc)

jarodsmk
  • 1,876
  • 2
  • 21
  • 40
  • But why do we receive a brand new instance while autowiring in the second case, but an existing one in the first case? – Daud Dec 22 '20 at 18:29
  • @Daud - what you are stating is not possible give the code your question. Please create a [example] that demonstrates the difference. – Randy Casburn Dec 22 '20 at 18:32
  • 1
    @Daud this may be due to the module resolution strategy, mentioned in the link posted. If you specify `.js` - it will try to interpret it as a `Node.js` module, however with the others, a `Classic` or plain Typescript module (which technically still needs to be compiled). Does that somewhat answer your question? – jarodsmk Dec 22 '20 at 18:34
  • @jarodsmk The interpretation as Node.js module makes sense. If you can update the answer with some documentation, it'll be great. Can't think of a way to replicate this on StackBlitz without resorting to routing or I would have :( – Daud Dec 22 '20 at 19:27