microsoft / microsoft/TypeScript

erasableSyntaxOnly: support commonjs require/exports in .cts files

Đang mở
#62,903 0 bình luận 1 reaction 1 người được giao Xem trên GitHub

@andrewbranch đang làm issue này rồi.

Từ ngày 6/1/2026.

Needs Investigation
Ngôn ngữ chính
Go
Star
111k
Fork
14.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

🔍 Search Terms

cts export

✅ Viability Checklist
⭐ Suggestion

Right now i can write (with type:module and allowJs:true):

module.cjs

module.exports.foo = 123;

index.ts

import { foo } from "./module.cjs"

and TypeScript correctly infers the type of the export foo (of course, it can have jsdoc types etc etc). Inside the .cjs file TS also correctly types result of require() calls.

However, if i do

module.cts

module.exports.foo = 123;

This no longer works. .cts file currently is supposed to have ESM style imports/exports that get transpiled to something.

However, such module.cts work perfectly fine with the node with type stripping - the types are stripped and the remaining code is processed as commonjs module (when package.json has type:module).

Suggestion - at least when erasableSyntaxOnly: true is specified, allow commonjs require/exports processing in .cts files, same as it happens in .cjs

📃 Motivating Example

This allows use of .cts files when escape from ESM to CJS is required in a project (for example, require() is needed to have conditional sync imports - yes, createRequire can be used in some cases, but not always).

It provides easier path to adopt typescript for node.js code when gradually converting existing codebases.

💻 Use Cases
  1. What do you want to use this for?

to run .cts files directly in node with type stripping.

  1. What shortcomings exist with current approaches?

we are forced to use .cjs with jsdoc type annotations, can't use .cts.

  1. What workarounds are you using in the meantime?

using .cjs with type annotations.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.