Skip to content
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

Allow override as parameter property #43831

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40621,9 +40621,6 @@ namespace ts {
else if (flags & ModifierFlags.Async) {
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_must_precede_1_modifier, "override", "async");
}
if (node.kind === SyntaxKind.Parameter) {
andrewbranch marked this conversation as resolved.
Show resolved Hide resolved
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_cannot_appear_on_a_parameter, "override");
}
flags |= ModifierFlags.Override;
lastOverride = modifier;
break;
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/override11.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/conformance/override/override11.ts(6,18): error TS1090: 'override' modifier cannot appear on a parameter.
tests/cases/conformance/override/override11.ts(6,27): error TS1029: 'public' modifier must precede 'override' modifier.


==== tests/cases/conformance/override/override11.ts (1 errors) ====
Expand All @@ -8,8 +8,8 @@ tests/cases/conformance/override/override11.ts(6,18): error TS1090: 'override' m

class Sub extends Base {
constructor (override public foo: number) {
~~~~~~~~
!!! error TS1090: 'override' modifier cannot appear on a parameter.
~~~~~~
!!! error TS1029: 'public' modifier must precede 'override' modifier.
super();
}
}
Expand Down
31 changes: 31 additions & 0 deletions tests/baselines/reference/overrideParameterProperty.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
tests/cases/conformance/override/overrideParameterProperty.ts(20,24): error TS1029: 'public' modifier must precede 'override' modifier.


==== tests/cases/conformance/override/overrideParameterProperty.ts (1 errors) ====
class Base {
p1!: string;
}

class C1 extends Base {
constructor(public override p1: "hello") {
super();
this.p1;
}
}

class C2 extends Base {
constructor(override p1: "hello") {
super();
this.p1;
}
}

class C3 extends Base {
constructor(override public p1: "hello") {
~~~~~~
!!! error TS1029: 'public' modifier must precede 'override' modifier.
super();
this.p1;
}
}

77 changes: 77 additions & 0 deletions tests/baselines/reference/overrideParameterProperty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//// [overrideParameterProperty.ts]
class Base {
p1!: string;
}

class C1 extends Base {
constructor(public override p1: "hello") {
super();
this.p1;
}
}

class C2 extends Base {
constructor(override p1: "hello") {
super();
this.p1;
}
}

class C3 extends Base {
constructor(override public p1: "hello") {
super();
this.p1;
}
}


//// [overrideParameterProperty.js]
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var Base = /** @class */ (function () {
function Base() {
}
return Base;
}());
var C1 = /** @class */ (function (_super) {
__extends(C1, _super);
function C1(p1) {
var _this = _super.call(this) || this;
_this.p1 = p1;
_this.p1;
return _this;
}
return C1;
}(Base));
var C2 = /** @class */ (function (_super) {
__extends(C2, _super);
function C2(p1) {
var _this = _super.call(this) || this;
_this.p1;
return _this;
}
return C2;
}(Base));
var C3 = /** @class */ (function (_super) {
__extends(C3, _super);
function C3(p1) {
var _this = _super.call(this) || this;
_this.p1 = p1;
_this.p1;
return _this;
}
return C3;
}(Base));
59 changes: 59 additions & 0 deletions tests/baselines/reference/overrideParameterProperty.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
=== tests/cases/conformance/override/overrideParameterProperty.ts ===
class Base {
>Base : Symbol(Base, Decl(overrideParameterProperty.ts, 0, 0))

p1!: string;
>p1 : Symbol(Base.p1, Decl(overrideParameterProperty.ts, 0, 12))
}

class C1 extends Base {
>C1 : Symbol(C1, Decl(overrideParameterProperty.ts, 2, 1))
>Base : Symbol(Base, Decl(overrideParameterProperty.ts, 0, 0))

constructor(public override p1: "hello") {
>p1 : Symbol(C1.p1, Decl(overrideParameterProperty.ts, 5, 14))

super();
>super : Symbol(Base, Decl(overrideParameterProperty.ts, 0, 0))

this.p1;
>this.p1 : Symbol(C1.p1, Decl(overrideParameterProperty.ts, 5, 14))
>this : Symbol(C1, Decl(overrideParameterProperty.ts, 2, 1))
>p1 : Symbol(C1.p1, Decl(overrideParameterProperty.ts, 5, 14))
}
}

class C2 extends Base {
>C2 : Symbol(C2, Decl(overrideParameterProperty.ts, 9, 1))
>Base : Symbol(Base, Decl(overrideParameterProperty.ts, 0, 0))

constructor(override p1: "hello") {
>p1 : Symbol(p1, Decl(overrideParameterProperty.ts, 12, 14))

super();
>super : Symbol(Base, Decl(overrideParameterProperty.ts, 0, 0))

this.p1;
>this.p1 : Symbol(Base.p1, Decl(overrideParameterProperty.ts, 0, 12))
>this : Symbol(C2, Decl(overrideParameterProperty.ts, 9, 1))
>p1 : Symbol(Base.p1, Decl(overrideParameterProperty.ts, 0, 12))
}
}

class C3 extends Base {
>C3 : Symbol(C3, Decl(overrideParameterProperty.ts, 16, 1))
>Base : Symbol(Base, Decl(overrideParameterProperty.ts, 0, 0))

constructor(override public p1: "hello") {
>p1 : Symbol(C3.p1, Decl(overrideParameterProperty.ts, 19, 14))

super();
>super : Symbol(Base, Decl(overrideParameterProperty.ts, 0, 0))

this.p1;
>this.p1 : Symbol(C3.p1, Decl(overrideParameterProperty.ts, 19, 14))
>this : Symbol(C3, Decl(overrideParameterProperty.ts, 16, 1))
>p1 : Symbol(C3.p1, Decl(overrideParameterProperty.ts, 19, 14))
}
}

62 changes: 62 additions & 0 deletions tests/baselines/reference/overrideParameterProperty.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
=== tests/cases/conformance/override/overrideParameterProperty.ts ===
class Base {
>Base : Base

p1!: string;
>p1 : string
}

class C1 extends Base {
>C1 : C1
>Base : Base

constructor(public override p1: "hello") {
>p1 : "hello"

super();
>super() : void
>super : typeof Base

this.p1;
>this.p1 : "hello"
>this : this
>p1 : "hello"
}
}

class C2 extends Base {
>C2 : C2
>Base : Base

constructor(override p1: "hello") {
>p1 : "hello"

super();
>super() : void
>super : typeof Base

this.p1;
>this.p1 : string
>this : this
>p1 : string
}
}

class C3 extends Base {
>C3 : C3
>Base : Base

constructor(override public p1: "hello") {
>p1 : "hello"

super();
>super() : void
>super : typeof Base

this.p1;
>this.p1 : "hello"
>this : this
>p1 : "hello"
}
}

26 changes: 26 additions & 0 deletions tests/cases/conformance/override/overrideParameterProperty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @noImplicitOverride: true

class Base {
p1!: string;
}

class C1 extends Base {
constructor(public override p1: "hello") {
super();
this.p1;
}
}

class C2 extends Base {
constructor(override p1: "hello") {
super();
this.p1;
}
}

class C3 extends Base {
constructor(override public p1: "hello") {
super();
this.p1;
}
}