5

Let's say in the src folder, we have two files, one is typescript file someFeatures.ts, the other is js file someFeatures.js,so their main file name is the same, and contain the same content,just the extension name is different, so in the main index.ts file, I import the dependency as:

import { sizeFormatter, costFormatter } from "./someFeatures";

so how does the compile know which one I'm referring to? it could be someFeatures.ts or someFeatures.js?

1 Answer 1

6

allowJS: false

If the allowJs option in tsconfig.json is set to false then Typescript compiler will ignore someFeatures.js like any .js file when it resolves imports. It will take only .ts .tsx .d.ts into account as explained here under How TypeScript resolves modules heading.

allowJS: true

If the allowJs option in tsconfig.json is set to true then Typescript compiler will specifically ignore someFeatures.js as a "possible output" due to presence of someFeatures.tsas explained there, search for "possible outputs".

Sign up to request clarification or add additional context in comments.

2 Comments

So in both cases someFeatures.js will be ignored by the TS compiler? Which case is now the one that should be used when in one file the .ts file should be imported but in another file a .js import is needed? Furthermore, there is no heading How TypeScript resolves modules for the first link, and searching for possible outputs does not find anything for the second link.
@winklerrr Yes, will be ignored in both cases. To import .js file (a) ensure its name doesn't conflict with a 'possible output', (b) provide an ambient declaration in a .d.ts file and (c) set allowJS: true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.