Skip to content

Fix export default dts widening#3352

Open
jakebailey wants to merge 2 commits intomainfrom
jabaile/dts-fixes-default-widening
Open

Fix export default dts widening#3352
jakebailey wants to merge 2 commits intomainfrom
jabaile/dts-fixes-default-widening

Conversation

@jakebailey
Copy link
Copy Markdown
Member

Two things:

  • For export assignments, prevent widening when serializing the symbol. This used to be done with preserveLiterals.
  • The "big shortcut" (which I'm pretty sure I suggested) breaks these literal cases, because we ask the checker to widen it.

Copilot AI review requested due to automatic review settings April 6, 2026 17:10
Copy link
Copy Markdown
Contributor

Copilot AI left a 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 fixes incorrect widening of literal types in emitted .d.ts for export default/export assignments, aligning tsgo’s declaration output and incremental behavior more closely with TypeScript’s (and removing previously-recorded expected diffs).

Changes:

  • Preserve literal types when serializing types for export assignments (avoids widening 1/"string"/42 to number/string).
  • Adjust pseudotype literal handling to avoid forcing widening via the “big shortcut”.
  • Update/clean up reference baselines and incremental test expectations now that diagnostics and signatures are correct.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
internal/checker/nodebuilderimpl.go Avoids widening literal types when serializing types for export assignments.
internal/checker/pseudotypenodebuilder.go Stops widening for pseudotype literal mapping to prevent incorrect literal loss.
internal/execute/tsctests/tsc_test.go Removes prior expectedDiff now that the underlying issue is fixed.
testdata/baselines/reference/tsc/incremental/change-to-type-that-gets-used-as-global-through-export-in-another-file.js Baseline update: default export retains literal type; incremental diagnostics/signatures updated.
testdata/baselines/reference/tsc/incremental/change-to-type-that-gets-used-as-global-through-export-in-another-file-through-indirect-import.js Baseline update: same as above, via re-export chain.
testdata/baselines/reference/tsc/composite/synthetic-jsx-import-of-ESM-module-from-CJS-module-no-crash-no-jsx-element.js Baseline update: default export literal type preserved (42).
testdata/baselines/reference/submodule/conformance/jsDeclarationsDefault(target=es2015).js Baseline update: default export literal type preserved (12).
testdata/baselines/reference/submodule/conformance/jsDeclarationsDefault(target=es2015).js.diff Baseline diff updated to reflect convergence.
testdata/baselines/reference/submodule/compiler/nodeNextCjsNamespaceImportDefault2.js Baseline update: default export string literal preserved ("string").
testdata/baselines/reference/submodule/compiler/nodeNextCjsNamespaceImportDefault2.js.diff Baseline diff removed/updated due to convergence.
testdata/baselines/reference/submodule/compiler/modulePreserve4.js Baseline update: preserves 0 literals for relevant default exports.
testdata/baselines/reference/submodule/compiler/modulePreserve4.js.diff Baseline diff updated to reflect convergence.
testdata/baselines/reference/submodule/compiler/dynamicImportsDeclaration.js Baseline update: preserves literal defaults (0, 1, "fallback").
testdata/baselines/reference/submodule/compiler/dynamicImportsDeclaration.js.diff Baseline diff removed/updated due to convergence.
testdata/baselines/reference/compiler/jsdocCommentDefaultExport(module=esnext).js Baseline update: preserves literal type in default export with JSDoc.
testdata/baselines/reference/compiler/jsdocCommentDefaultExport(module=commonjs).js Baseline update: preserves literal type in default export with JSDoc.

// `const _default: <type>`, and a const should preserve the literal type.
// Strada handles this via preserveLiterals in the syntactic type builder;
// we handle it by not widening the checker type in the first place.
if !ast.IsExportAssignment(declaration) {
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

serializeTypeForDeclaration skips literal widening only for ast.IsExportAssignment(declaration), but declaration can also be a KindJSExportAssignment (synthetic module.exports = ...) in the declarations transformer. Those cases also emit a const _default: <type> wrapper and should preserve literal types as well. Consider using ast.IsAnyExportAssignment(declaration) (or checking IsExportAssignment || IsJSExportAssignment) so CJS export-assignments don’t continue to widen.

Suggested change
if !ast.IsExportAssignment(declaration) {
if !ast.IsAnyExportAssignment(declaration) {

Copilot uses AI. Check for mistakes.
@jakebailey jakebailey requested a review from weswigham April 6, 2026 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants