Skip to content
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

Importing TypeScript file from JavaScript file with the .js extension fails. #5999

Open
6 tasks done
hornta opened this issue Jun 29, 2024 · 4 comments
Open
6 tasks done
Labels

Comments

@hornta
Copy link

hornta commented Jun 29, 2024

Describe the bug

When I import a TypeScript file from within a JavaScript file like so export { foo } from "./my-file.js"; (my-file is a .ts file on the FS). This should not error because TypeScript files can be imported using the JS extension.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-ivcrw5?file=test%2Fbasic.test.ts

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @vitest/ui: latest => 1.6.0 
    vite: latest => 5.3.2 
    vitest: latest => 1.6.0

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

This is Vite's limitation. It allows using .js extension for .ts files only if the importer (index.js in your case) is also a .ts file. If you disagree with this decision, you can start a discussion or open an issue in the vite repository.

@wparsons-heins-globality

Hey, the biggest problem with this is that it differentiates from how typescript is handling the same situation, so code that compiles just fine cannot be tested in the same way. Which for me was a big reason to drop jest for vitest in the first place.

It sounds like I'd now have to convert every single .js file to .ts to make sure we're never importing a .ts as .js, even though this is exactly what I have to do so support NodeNext module resolution.

If Vite does decide to keep it this way, is there anything we can do to get around it without migrating .js to .ts?

@nkeil
Copy link

nkeil commented Oct 14, 2024

Unfortunate to see this not supported, but seems like it's being worked on here: vitejs/vite#8993

@vjpr
Copy link

vjpr commented Nov 21, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants