question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support Typescript 4.7 "Node16" extension requirements

See original GitHub issue

Description

With Typescript version 4.7, Typescript now has the option to preserve “ES Modules” if tsconfig.json specifies:

{
  "compilerOptions": {
    "module": "Node16"
  }
}

This is a step forward in supporting native ESM in Node from Typescript’s direction, but it comes with the requirement that all typescript modules when imported use the .js extension.

Ref: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html

This works all fine and well in Vite when importing typescript modules in .ts-files.

The problems begin when importing .ts-files in .js, .vue, .svelte, and so on.

E.g.

// ./some-ts-file.ts
export const hello = "Hello";

// ./my-app.js
import { hello } from "./some-ts-file.js"

See reproducible example: https://github.com/aMediocreDad/vite-import-ts-as-js

Suggested solution

The expectation is that importing .ts-files as .js resolves the file like it does in .ts-files at the moment (See related “Closed” issue: https://github.com/vitejs/vite/issues/3040).

Alternative

No response

Additional context

No response

Validations

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:7
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
bluwycommented, Oct 14, 2022

We’ve discussed this in the last team meeting and decided to support .js -> .ts resolving for JS files too based on the related tsconfig.json/jsconfig.json that has the special resolution setting.

The rule seems to be that, if the moduleResolution is node16, nodenext, or above; Or else if the module is node16, nodenext, or above; Then we’d kick in the file resolution.

3reactions
aMediocreDadcommented, Jul 10, 2022

Out of curiosity, did you notice improvements? Runtime or Buildtime?

I have not tested for any build/runtime improvements. However, it does provide some long awaited developer experience improvements.

Using the “Node16” module specifier makes Typescript finally use the import/export fields in package.json properly. This is a big help where packages don’t use the “main” field because they do not want to indicate backwards compatibility with pre Node 12. Previously, Typescript tooling was usually difficult to set up because of how it failed to recognize newer package.json configurations (that only cater to ESM).

I do not use the tsc compiler so I won’t see the improvements that the compilation options provide. But the steady move towards proper ESM handling across all tools commonly used in the same toolchain is all I really want to see.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - TypeScript 4.7
Node.js supports two extensions to help with this: .mjs and .cjs . .mjs files are always ES modules, and .cjs files are always...
Read more >
Understanding TypeScript 4.7 and ECMAScript module support
The TypeScript 4.7 release comes a major upgrade to ECMAScript module support for Node.js, allowing developers to better support modules.
Read more >
Announcing TypeScript 4.7 - Microsoft Developer Blogs
In our beta release, we announced a stable target for our Node ESM support called node12 ; however, Node.js 12 is no longer...
Read more >
"module": "node16" should support extension rewriting - Reddit
63 votes, 26 comments. With the release of TypeScript 4.7 approaching soon, we finally gain direct support for ESM in Node.
Read more >
Add file extension when '--moduleResolution' is 'node16' or ...
TypeScript 4.7 : ECMAScript Module Support: Add file extension when '--moduleResolution' is 'node16' or 'nodenext'. Relates to 1.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found