-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Cannot export both types and values from CJS #60852
Comments
Followup, I have come across the following excerpt from #52203:
Given this information it's apparently a known issue that CJS export syntax is painful. I'd consider this issue a request to make it less painful. While the quoted excerpt above implies that CJS is not a first-class citizen of
Similarly on the issue proposal:
My personal initial impression working with Anyways, so I would strongly request some improvements around DevX. Ideas for making
Both of these would be equivalent to the "merged namespace" approach. With CJS it's very important IMO to be extremely clear what the exported value is. So I'm happy for types and values not to be allowed to mix in the exports. But I don't see any reason why type-only export syntax which is currently banned should not be allowed to exist in parallel to the value export syntax. Users don't expect type-only syntax to have runtime impact, so I don't see the possibility for confusion. Similarly, on the
|
🔎 Search Terms
verbatimModuleSyntax, commonjs, require, export, export type,
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?moduleResolution=99&target=99&module=199&verbatimModuleSyntax=true&ts=5.8.0-dev.20241225#code/KYDwDg9gTgLgBDAnmYcBiEJwLxwM4xQCWAdgOZwA+cJArgLYBGwUA3AFDsDGANgIZ48cAEJ8ocAN4BfTqEiwcIsayA
💻 Code
I wanted to convert a *.ts file that gets interpreted as CJS (due to package.json "type" field) to use
verbatimModuleSyntax
for explicitness.The file previously looked like this
and could be imported like so
After enabling
verbatimModuleSyntax
, it seems no longer possible to export bothFoo
andBar
from index.ts. Some attempts:This is ok,
but this is not
nor this
tsconfig:
package.json: has
"type": "commonjs"
🙁 Actual behavior
Can't find a way to export both types and values using CJS import/export syntax required by verbatimModuleSyntax
🙂 Expected behavior
There is a way to export both types and values using CJS import/export syntax
Additional information about the issue
Perhaps this is possible, and if so I'd ask it be added to the documentation. Without this, a currently-functioning CJS module cannot straightforwardly be converted to use verbatimModuleSyntax.
FWIW, if it's not possible, I don't see why at least one of the following wouldn't be allowed (specifically due to the use of
export type
to ensure only one value export exists)or
The text was updated successfully, but these errors were encountered: