Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Cannot export both types and values from CJS #60852

Open
Assignees
andrewbranch
Labels
In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript
@kirkwaiblinger

Description

@kirkwaiblinger

🔎 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 useverbatimModuleSyntax for explicitness.

The file previously looked like this

// index.tsexporttypeFoo=string|number;exportconstBar=42;

and could be imported like so

// foo.tsimport{typeFoo,Bar}from'./index';constx:Foo=42;consty=Bar;

After enablingverbatimModuleSyntax, it seems no longer possible to export bothFoo andBar from index.ts. Some attempts:

typeFoo=string|number;constBar=42;export={ Foo, Bar};// error; Foo is a type but used as a value

This is ok,

exporttypeFoo=string|number;constBar=42;

but this is not

exporttypeFoo=string|number;constBar=42;export={ Bar};// An export assignment cannot be used in a module with other exported elements. (TS2309)

nor this

typeFoo=string|number;constBar=42;exporttype{Foo};export={ Bar};// An export assignment cannot be used in a module with other exported elements. (TS2309)

tsconfig:

{"compilerOptions": {"verbatimModuleSyntax":true,"target":"ESNext","module":"NodeNext","strict":true,    }}

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 ofexport type to ensure only onevalue export exists)

exporttypeFoo=string|number;constBar=42;export={ Bar};// not a problem; only one value export

or

typeFoo=string|number;constBar=42;exporttype{Foo};export={ Bar};

Metadata

Metadata

Assignees

Labels

In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp