microsoft / microsoft/TypeScript

Cannot export both types and values from CJS

オープン
#60,852 コメント 2 件 リアクション 1 件 担当者 1 名 GitHub で見る

@andrewbranch がすでに取り組んでいます。

2025年1月3日 から。

In Discussion Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

🔎 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

// index.ts
export type Foo = string | number;
export const Bar = 42;

and could be imported like so

// foo.ts
import { type Foo, Bar } from './index';

const x: Foo = 42;
const y = Bar;

After enabling verbatimModuleSyntax, it seems no longer possible to export both Foo and Bar from index.ts. Some attempts:

type Foo = string | number;
const Bar = 42;

export = { Foo, Bar }; // error; Foo is a type but used as a value

This is ok,

export type Foo = string | number;
const Bar = 42;

but this is not

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

nor this

type Foo = string | number;
const Bar = 42;
export type { 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 of export type to ensure only one value export exists)

export type Foo = string | number;
const Bar = 42;
export = { Bar }; // not a problem; only one value export

or

type Foo = string | number;
const Bar = 42;
export type { Foo };
export = { Bar };

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。