-
Notifications
You must be signed in to change notification settings - Fork 13k
Give more specific errors for verbatimModuleSyntax
#62113
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR improves error messages for the verbatimModuleSyntax
TypeScript compiler option to provide more specific and actionable guidance to developers. The change replaces generic "ESM syntax is not allowed" messages with detailed explanations that include specific configuration suggestions.
Key Changes
- Replaces generic TS1286 error message with a new, more detailed TS1295 error message for
verbatimModuleSyntax
violations - Adds conditional logic to provide different error messages based on file extensions (.cts/.cjs vs other files)
- Updates error message for
module: preserve
mode to be more descriptive
Reviewed Changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
src/compiler/diagnosticMessages.json | Adds new detailed error message (TS1295) and updates existing messages to be more specific |
src/compiler/checker.ts | Implements logic to conditionally show different error messages based on file type and adds helper function |
tests/baselines/reference/*.errors.txt | Updates test baselines to reflect the new, more detailed error messages |
src/compiler/checker.ts
Outdated
// Check if the file is .cts or .cjs (CommonJS-specific extensions) | ||
if (fileExtensionIsOneOf(fileName, [Extension.Cts, Extension.Cjs])) { | ||
return Diagnostics.ECMAScript_imports_and_exports_cannot_be_written_in_a_CommonJS_file_under_verbatimModuleSyntax; | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message for .cts/.cjs files should provide guidance on how to resolve the issue, similar to the detailed message used for other file types. Currently it only states what's wrong without offering solutions.
} else { | |
if (fileExtensionIsOneOf(fileName, [Extension.Cts, Extension.Cjs])) { | |
return Diagnostics.ECMAScript_imports_and_exports_cannot_be_written_in_a_CommonJS_file_under_verbatimModuleSyntax_Adjust_the_type_field_in_the_nearest_package_json_to_make_this_file_an_ECMAScript_module_or_adjust_your_verbatimModuleSyntax_module_and_moduleResolution_settings_in_TypeScript; | |
} else { |
Copilot uses AI. Check for mistakes.
Especially given that
tsc --init
initializes with VMS.