Skip to content

Inconsistent diagnostic and misleading error span for 'override declare' (TS1040 vs TS1243) #65399

Open

Description

🔎 Search Terms

override declare, TS1040 override, override modifier cannot be used in an ambient context

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about class modifiers.

⏯ Playground Link

No response

💻 Code

class Base {
p1: number = 1;
p2: number = 2;
}

class Derived extends Base {
declare override p1: number;
override declare p2: number;
}

🙁 Actual behavior

When written as override declare, TypeScript reports:

override declare p2: number; ~~~~~~~ error TS1040: 'override' modifier cannot be used in an ambient context.

  1. Incorrect message: override is explicitly allowed in ambient contexts (declare class Derived extends Base { override p: number; } is valid in both .ts and .d.ts files).
  2. Misplaced error anchor: The squiggly underlines declare, but mentions override and claims the context is ambient (even in an ordinary non-ambient class).
  3. Order asymmetry: Writing declare override reports TS1243: 'override' modifier cannot be used with 'declare' modifier., but flipping the order yields TS1040.

🙂 Expected behavior

Combining declare and override on a class property should report TS1243: '{0}' modifier cannot be used with '{1}' modifier. symmetrically regardless of order:

class Derived extends Base {
    declare override p1: number; // TS1243: 'override' modifier cannot be used with 'declare' modifier.
    override declare p2: number; // Expected TS1243: 'declare' modifier cannot be used with 'override' modifier.
}

In tsc/internal/checker/grammarchecks.go, line 451 currently calls diagnostics.X_0_modifier_cannot_be_used_in_an_ambient_context with "override". Updating it to diagnostics.X_0_modifier_cannot_be_used_with_1_modifier, "declare", "override" restores symmetry with other mutually exclusive modifier combinations (such as declare accessor / accessor declare).

Additional information about the issue

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Error MessagesThe issue relates to error messaging

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions