Skip to content

Allow accessor properties in ambient contexts when targeting ES5 #61927

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 23, 2025

This PR fixes an issue where TypeScript incorrectly disallowed accessor properties in ambient contexts (.d.ts files, declare class, declare module, etc.) when targeting ES5.

Problem

Previously, TypeScript would error on accessor properties in all contexts when targeting ES5:

// @target: es5

// This would error even though it's ambient (no runtime code generated)
declare class LibraryClass {
    accessor options: any; // ❌ Error: Properties with the 'accessor' modifier are only available when targeting ECMAScript 2015 and higher
}

// This would also error
declare module "some-library" {
    export class Parser {
        accessor config: any; // ❌ Same error
    }
}

// This should still error (runtime code needs to be generated)
class UserClass {
    accessor prop: string; // ❌ Should still error
}

This forced consumers of libraries like @babel/parser (which use accessor in their .d.ts files) to enable skipLibCheck when targeting ES5, which is not ideal.

Solution

Modified the checkGrammarProperty function in src/compiler/checker.ts to exclude ambient contexts from the ES5 accessor restriction:

// Before
if (languageVersion < ScriptTarget.ES2015 && isAutoAccessorPropertyDeclaration(node)) {
    return grammarErrorOnNode(node.name, Diagnostics.Properties_with_the_accessor_modifier_are_only_available_when_targeting_ECMAScript_2015_and_higher);
}

// After  
if (languageVersion < ScriptTarget.ES2015 && isAutoAccessorPropertyDeclaration(node) && !(node.flags & NodeFlags.Ambient)) {
    return grammarErrorOnNode(node.name, Diagnostics.Properties_with_the_accessor_modifier_are_only_available_when_targeting_ECMAScript_2015_and_higher);
}

Result

With this fix:

// @target: es5

// ✅ No longer errors - ambient contexts are allowed
declare class LibraryClass {
    accessor options: any;
    static accessor defaults: any;
}

declare module "some-library" {
    export class Parser {
        accessor config: any;
    }
}

// ❌ Still correctly errors - regular classes targeting ES5 cannot use accessor
class UserClass {
    accessor prop: string; // Error: Properties with the 'accessor' modifier are only available when targeting ECMAScript 2015 and higher
}

Testing

  • Added comprehensive test case accessorInAmbientContextES5.ts covering all ambient context scenarios
  • Verified all existing autoAccessor tests continue to pass (252 tests)
  • Confirmed regular classes still error correctly when targeting ES5
  • No regressions detected in broader test runs

This change follows the same pattern used elsewhere in TypeScript for handling ES2015+ features in ambient contexts and maintains backward compatibility.

Fixes #61181.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • plugins.dprint.dev
    • Triggering command: /home/REDACTED/work/TypeScript/TypeScript/node_modules/dprint/dprint fmt (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@typescript-bot typescript-bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Jun 23, 2025
@Copilot Copilot AI changed the title [WIP] Allow accessor properties in .d.ts even when targeting ES5 Allow accessor properties in ambient contexts when targeting ES5 Jun 23, 2025
Copilot finished work on behalf of RyanCavanaugh June 23, 2025 22:40
@Copilot Copilot AI requested a review from RyanCavanaugh June 23, 2025 22:40
Copy link
Member

@RyanCavanaugh RyanCavanaugh left a comment

Choose a reason for hiding this comment

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

Fix your line endings in checker.ts to match the rest of the file

Copy link
Contributor Author

Copilot AI commented Jun 24, 2025

Fix your line endings in checker.ts to match the rest of the file

Fixed line endings to match CRLF format used throughout checker.ts file. c8adde3

@Copilot Copilot AI requested a review from RyanCavanaugh June 24, 2025 16:04
Copilot finished work on behalf of RyanCavanaugh June 24, 2025 16:04
@RyanCavanaugh RyanCavanaugh marked this pull request as ready for review June 24, 2025 16:30
Copy link
Member

@RyanCavanaugh RyanCavanaugh left a comment

Choose a reason for hiding this comment

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

🏆

@RyanCavanaugh RyanCavanaugh enabled auto-merge (squash) June 24, 2025 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow accessor properties in .d.ts even when targeting ES5
4 participants