Skip to content

Fix declaration emit dropping readonly from as const object literal properties#3933

Open
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-tsgo-declaration-emit
Open

Fix declaration emit dropping readonly from as const object literal properties#3933
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-tsgo-declaration-emit

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 16, 2026

as const object literals were missing readonly on method shorthands and outer properties in declaration emit:

export const value = {
  method(): string { return "a"; },
  prop: { nested: 1 },
} as const;

// tsgo emitted (wrong):
//   method(): string;
//   prop: { readonly nested: 1; };

// expected:
//   readonly method: () => string;
//   readonly prop: { readonly nested: 1; };

Analysis

pseudotypenodebuilder.go determines const context via isConstContext(elements[0].Name). That function walks the parent chain but only propagates through PropertyAssignment/ShorthandPropertyAssignment — not MethodDeclaration. When the first element is a method, the traversal dead-ends and isConst is false, dropping readonly from all properties.

Fix

Pass elements[0].Name.Parent.Parent (the ObjectLiteralExpression) instead of the property name. The object literal's parent is directly the as const assertion, so isConstContext resolves correctly regardless of which property kind comes first.

Copilot AI and others added 2 commits May 16, 2026 20:55
…properties

Fix pseudotypenodebuilder to use the ObjectLiteralExpression node instead of the
property name when checking isConstContext. The property name's parent could be a
MethodDeclaration which isConstContext doesn't traverse through, causing readonly
to be dropped from method shorthands and outer properties in as const objects.

Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/a6a59cb3-682f-426b-8fed-1bddcd23ab2c

Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix tsgo declaration emit to retain readonly for method shorthands Fix declaration emit dropping readonly from as const object literal properties May 16, 2026
Copilot AI requested a review from jakebailey May 16, 2026 21:10
@jakebailey jakebailey marked this pull request as ready for review May 16, 2026 22:20
Copilot AI review requested due to automatic review settings May 16, 2026 22:20
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

Fixes a declaration emit regression in the Go port where as const object literals could lose readonly on all properties when the first property was a method shorthand, by ensuring const-context detection starts from the object literal node rather than the first property’s name.

Changes:

  • Update pseudo type node building to compute isConst from the containing ObjectLiteralExpression (so method shorthands don’t break const-context propagation).
  • Add a new compiler test + reference baselines covering as const with a method shorthand and nested object property.
  • Update an existing reference baseline (declarationEmitConstObjectLiteralGenericMethod1) to reflect correct readonly + function-type property emission for const object literal methods.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
internal/checker/pseudotypenodebuilder.go Fixes const-context detection for pseudo object literal emission by anchoring the check at the object literal node.
testdata/tests/cases/compiler/declarationEmitReadonlyAsConst.ts Adds regression test reproducing the missing-readonly behavior with a leading method shorthand.
testdata/baselines/reference/compiler/declarationEmitReadonlyAsConst.types Reference type baseline for the new test.
testdata/baselines/reference/compiler/declarationEmitReadonlyAsConst.symbols Reference symbol baseline for the new test.
testdata/baselines/reference/compiler/declarationEmitReadonlyAsConst.js Reference JS + .d.ts baseline verifying readonly properties and function-type method emission.
testdata/baselines/reference/compiler/declarationEmitConstObjectLiteralGenericMethod1.js Updates .d.ts reference output to match corrected readonly/function-type emission for const object literal generic methods.

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.

tsgo declaration emit drops readonly from method shorthands and outer properties in as const object literal

3 participants