Skip to content
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

Support type checking .cjs files #38784

Closed
5 tasks done
ExE-Boss opened this issue May 26, 2020 · 5 comments
Closed
5 tasks done

Support type checking .cjs files #38784

ExE-Boss opened this issue May 26, 2020 · 5 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@ExE-Boss
Copy link
Contributor

ExE-Boss commented May 26, 2020

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

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Jun 4, 2020
@frank-dspeed
Copy link

frank-dspeed commented Aug 7, 2020

If some one does want to test #39840 https://github.com/stealify/typescript

npm i -g vscode-typescript@https://github.com/stealify/typescript
# Getting global path
npm root -g

Hit f1 on keyboard open user settings and set

"typescript.tsdk": "{your_global_npm_path}/vscode-typescript/typescript/lib"

restart is not needed.

@Raynos
Copy link

Raynos commented Feb 1, 2021

I would like to use the .cjs & .mjs extensions with typescript as well. All three of these files are JavaScript files.

@a-ejs
Copy link

a-ejs commented Feb 10, 2021

Agreed, seems odd that this isn't supported (or even planned?) yet.

@hariombalhara
Copy link

For people coming to this thread, you can add //@ts-check at the top of a .cjs file to enable type checking for it.

@ExE-Boss
Copy link
Contributor Author

Fixed in #45884 (TypeScript 4.5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
6 participants