Skip to content

Support type checking .cjs files #38784

Closed
@ExE-Boss

Description

@ExE-Boss

Search Terms

  • cjs
  • cjs extension
  • cjs files

Suggestion

Treat files with the .cjs file extension the same as the .js file extension, but disallowing ES Module syntax within them.

Use Cases

In packages with:

"type": "module",

in package.json, Node loads all .js files as ES modules.

To load a file as a CommonJS module, one has to use the .cjs file extension.

Examples

// foo.cjs

// $ExpectType typeof Bar
const Bar = require("./bar.cjs");

📝 Note: In order to currently have typed .cjs imports, this file has to be named bar.cjs.d.ts:

// bar.d.ts
declare class Bar {
	constructor();
}
export = Bar;
// bar.cjs
module.exports = class Bar {
	constructor() {
		// do stuff
	}
};

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions