You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inconsistent diagnostic and misleading error span for 'override declare' (TS1040 vs TS1243) #65399
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.
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).
Misplaced error anchor: The squiggly underlines declare, but mentions override and claims the context is ambient (even in an ordinary non-ambient class).
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:
classDerivedextendsBase{declareoverridep1: number;// TS1243: 'override' modifier cannot be used with 'declare' modifier.overridedeclarep2: 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).
Activity