diff --git a/tests/baselines/reference/optionalMethods.js b/tests/baselines/reference/optionalMethods.js index 1cf09a588a7..42decf512e4 100644 --- a/tests/baselines/reference/optionalMethods.js +++ b/tests/baselines/reference/optionalMethods.js @@ -20,6 +20,8 @@ function test1(x: Foo) { class Bar { a: number; b?: number; + c? = 2; + constructor(public d?: number, public e = 10) {} f() { return 1; } @@ -32,6 +34,9 @@ class Bar { function test2(x: Bar) { x.a; x.b; + x.c; + x.d; + x.e; x.f; x.g; let f1 = x.f(); @@ -40,9 +45,24 @@ function test2(x: Bar) { let h1 = x.h && x.h(); let h2 = x.h ? x.h() : 0; } + +class Base { + a?: number; + f?(): number; +} + +class Derived extends Base { + a = 1; + f(): number { return 1; } +} //// [optionalMethods.js] +var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +}; function test1(x) { x.a; x.b; @@ -53,7 +73,11 @@ function test1(x) { var g2 = x.g ? x.g() : 0; } var Bar = (function () { - function Bar() { + function Bar(d, e) { + if (e === void 0) { e = 10; } + this.d = d; + this.e = e; + this.c = 2; } Bar.prototype.f = function () { return 1; @@ -66,6 +90,9 @@ var Bar = (function () { function test2(x) { x.a; x.b; + x.c; + x.d; + x.e; x.f; x.g; var f1 = x.f(); @@ -74,3 +101,47 @@ function test2(x) { var h1 = x.h && x.h(); var h2 = x.h ? x.h() : 0; } +var Base = (function () { + function Base() { + } + return Base; +}()); +var Derived = (function (_super) { + __extends(Derived, _super); + function Derived() { + _super.apply(this, arguments); + this.a = 1; + } + Derived.prototype.f = function () { return 1; }; + return Derived; +}(Base)); + + +//// [optionalMethods.d.ts] +interface Foo { + a: number; + b?: number; + f(): number; + g?(): number; +} +declare function test1(x: Foo): void; +declare class Bar { + d?: number; + e: number; + a: number; + b?: number; + c?: number | undefined; + constructor(d?: number, e?: number); + f(): number; + g?(): number; + h?(): number; +} +declare function test2(x: Bar): void; +declare class Base { + a?: number; + f?(): number; +} +declare class Derived extends Base { + a: number; + f(): number; +} diff --git a/tests/baselines/reference/optionalMethods.symbols b/tests/baselines/reference/optionalMethods.symbols index 6b06e1902f4..55c5ec958da 100644 --- a/tests/baselines/reference/optionalMethods.symbols +++ b/tests/baselines/reference/optionalMethods.symbols @@ -75,86 +75,129 @@ class Bar { b?: number; >b : Symbol(Bar.b, Decl(optionalMethods.ts, 19, 14)) + c? = 2; +>c : Symbol(Bar.c, Decl(optionalMethods.ts, 20, 15)) + + constructor(public d?: number, public e = 10) {} +>d : Symbol(Bar.d, Decl(optionalMethods.ts, 22, 16)) +>e : Symbol(Bar.e, Decl(optionalMethods.ts, 22, 34)) + f() { ->f : Symbol(Bar.f, Decl(optionalMethods.ts, 20, 15)) +>f : Symbol(Bar.f, Decl(optionalMethods.ts, 22, 52)) return 1; } g?(): number; // Body of optional method can be omitted ->g : Symbol(Bar.g, Decl(optionalMethods.ts, 23, 5)) +>g : Symbol(Bar.g, Decl(optionalMethods.ts, 25, 5)) h?() { ->h : Symbol(Bar.h, Decl(optionalMethods.ts, 24, 17)) +>h : Symbol(Bar.h, Decl(optionalMethods.ts, 26, 17)) return 2; } } function test2(x: Bar) { ->test2 : Symbol(test2, Decl(optionalMethods.ts, 28, 1)) ->x : Symbol(x, Decl(optionalMethods.ts, 30, 15)) +>test2 : Symbol(test2, Decl(optionalMethods.ts, 30, 1)) +>x : Symbol(x, Decl(optionalMethods.ts, 32, 15)) >Bar : Symbol(Bar, Decl(optionalMethods.ts, 16, 1)) x.a; >x.a : Symbol(Bar.a, Decl(optionalMethods.ts, 18, 11)) ->x : Symbol(x, Decl(optionalMethods.ts, 30, 15)) +>x : Symbol(x, Decl(optionalMethods.ts, 32, 15)) >a : Symbol(Bar.a, Decl(optionalMethods.ts, 18, 11)) x.b; >x.b : Symbol(Bar.b, Decl(optionalMethods.ts, 19, 14)) ->x : Symbol(x, Decl(optionalMethods.ts, 30, 15)) +>x : Symbol(x, Decl(optionalMethods.ts, 32, 15)) >b : Symbol(Bar.b, Decl(optionalMethods.ts, 19, 14)) + x.c; +>x.c : Symbol(Bar.c, Decl(optionalMethods.ts, 20, 15)) +>x : Symbol(x, Decl(optionalMethods.ts, 32, 15)) +>c : Symbol(Bar.c, Decl(optionalMethods.ts, 20, 15)) + + x.d; +>x.d : Symbol(Bar.d, Decl(optionalMethods.ts, 22, 16)) +>x : Symbol(x, Decl(optionalMethods.ts, 32, 15)) +>d : Symbol(Bar.d, Decl(optionalMethods.ts, 22, 16)) + + x.e; +>x.e : Symbol(Bar.e, Decl(optionalMethods.ts, 22, 34)) +>x : Symbol(x, Decl(optionalMethods.ts, 32, 15)) +>e : Symbol(Bar.e, Decl(optionalMethods.ts, 22, 34)) + x.f; ->x.f : Symbol(Bar.f, Decl(optionalMethods.ts, 20, 15)) ->x : Symbol(x, Decl(optionalMethods.ts, 30, 15)) ->f : Symbol(Bar.f, Decl(optionalMethods.ts, 20, 15)) +>x.f : Symbol(Bar.f, Decl(optionalMethods.ts, 22, 52)) +>x : Symbol(x, Decl(optionalMethods.ts, 32, 15)) +>f : Symbol(Bar.f, Decl(optionalMethods.ts, 22, 52)) x.g; ->x.g : Symbol(Bar.g, Decl(optionalMethods.ts, 23, 5)) ->x : Symbol(x, Decl(optionalMethods.ts, 30, 15)) ->g : Symbol(Bar.g, Decl(optionalMethods.ts, 23, 5)) +>x.g : Symbol(Bar.g, Decl(optionalMethods.ts, 25, 5)) +>x : Symbol(x, Decl(optionalMethods.ts, 32, 15)) +>g : Symbol(Bar.g, Decl(optionalMethods.ts, 25, 5)) let f1 = x.f(); ->f1 : Symbol(f1, Decl(optionalMethods.ts, 35, 7)) ->x.f : Symbol(Bar.f, Decl(optionalMethods.ts, 20, 15)) ->x : Symbol(x, Decl(optionalMethods.ts, 30, 15)) ->f : Symbol(Bar.f, Decl(optionalMethods.ts, 20, 15)) +>f1 : Symbol(f1, Decl(optionalMethods.ts, 40, 7)) +>x.f : Symbol(Bar.f, Decl(optionalMethods.ts, 22, 52)) +>x : Symbol(x, Decl(optionalMethods.ts, 32, 15)) +>f : Symbol(Bar.f, Decl(optionalMethods.ts, 22, 52)) let g1 = x.g && x.g(); ->g1 : Symbol(g1, Decl(optionalMethods.ts, 36, 7)) ->x.g : Symbol(Bar.g, Decl(optionalMethods.ts, 23, 5)) ->x : Symbol(x, Decl(optionalMethods.ts, 30, 15)) ->g : Symbol(Bar.g, Decl(optionalMethods.ts, 23, 5)) ->x.g : Symbol(Bar.g, Decl(optionalMethods.ts, 23, 5)) ->x : Symbol(x, Decl(optionalMethods.ts, 30, 15)) ->g : Symbol(Bar.g, Decl(optionalMethods.ts, 23, 5)) +>g1 : Symbol(g1, Decl(optionalMethods.ts, 41, 7)) +>x.g : Symbol(Bar.g, Decl(optionalMethods.ts, 25, 5)) +>x : Symbol(x, Decl(optionalMethods.ts, 32, 15)) +>g : Symbol(Bar.g, Decl(optionalMethods.ts, 25, 5)) +>x.g : Symbol(Bar.g, Decl(optionalMethods.ts, 25, 5)) +>x : Symbol(x, Decl(optionalMethods.ts, 32, 15)) +>g : Symbol(Bar.g, Decl(optionalMethods.ts, 25, 5)) let g2 = x.g ? x.g() : 0; ->g2 : Symbol(g2, Decl(optionalMethods.ts, 37, 7)) ->x.g : Symbol(Bar.g, Decl(optionalMethods.ts, 23, 5)) ->x : Symbol(x, Decl(optionalMethods.ts, 30, 15)) ->g : Symbol(Bar.g, Decl(optionalMethods.ts, 23, 5)) ->x.g : Symbol(Bar.g, Decl(optionalMethods.ts, 23, 5)) ->x : Symbol(x, Decl(optionalMethods.ts, 30, 15)) ->g : Symbol(Bar.g, Decl(optionalMethods.ts, 23, 5)) +>g2 : Symbol(g2, Decl(optionalMethods.ts, 42, 7)) +>x.g : Symbol(Bar.g, Decl(optionalMethods.ts, 25, 5)) +>x : Symbol(x, Decl(optionalMethods.ts, 32, 15)) +>g : Symbol(Bar.g, Decl(optionalMethods.ts, 25, 5)) +>x.g : Symbol(Bar.g, Decl(optionalMethods.ts, 25, 5)) +>x : Symbol(x, Decl(optionalMethods.ts, 32, 15)) +>g : Symbol(Bar.g, Decl(optionalMethods.ts, 25, 5)) let h1 = x.h && x.h(); ->h1 : Symbol(h1, Decl(optionalMethods.ts, 38, 7)) ->x.h : Symbol(Bar.h, Decl(optionalMethods.ts, 24, 17)) ->x : Symbol(x, Decl(optionalMethods.ts, 30, 15)) ->h : Symbol(Bar.h, Decl(optionalMethods.ts, 24, 17)) ->x.h : Symbol(Bar.h, Decl(optionalMethods.ts, 24, 17)) ->x : Symbol(x, Decl(optionalMethods.ts, 30, 15)) ->h : Symbol(Bar.h, Decl(optionalMethods.ts, 24, 17)) +>h1 : Symbol(h1, Decl(optionalMethods.ts, 43, 7)) +>x.h : Symbol(Bar.h, Decl(optionalMethods.ts, 26, 17)) +>x : Symbol(x, Decl(optionalMethods.ts, 32, 15)) +>h : Symbol(Bar.h, Decl(optionalMethods.ts, 26, 17)) +>x.h : Symbol(Bar.h, Decl(optionalMethods.ts, 26, 17)) +>x : Symbol(x, Decl(optionalMethods.ts, 32, 15)) +>h : Symbol(Bar.h, Decl(optionalMethods.ts, 26, 17)) let h2 = x.h ? x.h() : 0; ->h2 : Symbol(h2, Decl(optionalMethods.ts, 39, 7)) ->x.h : Symbol(Bar.h, Decl(optionalMethods.ts, 24, 17)) ->x : Symbol(x, Decl(optionalMethods.ts, 30, 15)) ->h : Symbol(Bar.h, Decl(optionalMethods.ts, 24, 17)) ->x.h : Symbol(Bar.h, Decl(optionalMethods.ts, 24, 17)) ->x : Symbol(x, Decl(optionalMethods.ts, 30, 15)) ->h : Symbol(Bar.h, Decl(optionalMethods.ts, 24, 17)) +>h2 : Symbol(h2, Decl(optionalMethods.ts, 44, 7)) +>x.h : Symbol(Bar.h, Decl(optionalMethods.ts, 26, 17)) +>x : Symbol(x, Decl(optionalMethods.ts, 32, 15)) +>h : Symbol(Bar.h, Decl(optionalMethods.ts, 26, 17)) +>x.h : Symbol(Bar.h, Decl(optionalMethods.ts, 26, 17)) +>x : Symbol(x, Decl(optionalMethods.ts, 32, 15)) +>h : Symbol(Bar.h, Decl(optionalMethods.ts, 26, 17)) +} + +class Base { +>Base : Symbol(Base, Decl(optionalMethods.ts, 45, 1)) + + a?: number; +>a : Symbol(Base.a, Decl(optionalMethods.ts, 47, 12)) + + f?(): number; +>f : Symbol(Base.f, Decl(optionalMethods.ts, 48, 15)) +} + +class Derived extends Base { +>Derived : Symbol(Derived, Decl(optionalMethods.ts, 50, 1)) +>Base : Symbol(Base, Decl(optionalMethods.ts, 45, 1)) + + a = 1; +>a : Symbol(Derived.a, Decl(optionalMethods.ts, 52, 28)) + + f(): number { return 1; } +>f : Symbol(Derived.f, Decl(optionalMethods.ts, 53, 10)) } diff --git a/tests/baselines/reference/optionalMethods.types b/tests/baselines/reference/optionalMethods.types index 528a2e23540..70fda27c012 100644 --- a/tests/baselines/reference/optionalMethods.types +++ b/tests/baselines/reference/optionalMethods.types @@ -81,6 +81,15 @@ class Bar { b?: number; >b : number | undefined + c? = 2; +>c : number | undefined +>2 : number + + constructor(public d?: number, public e = 10) {} +>d : number | undefined +>e : number +>10 : number + f() { >f : () => number @@ -113,6 +122,21 @@ function test2(x: Bar) { >x : Bar >b : number | undefined + x.c; +>x.c : number | undefined +>x : Bar +>c : number | undefined + + x.d; +>x.d : number | undefined +>x : Bar +>d : number | undefined + + x.e; +>x.e : number +>x : Bar +>e : number + x.f; >x.f : () => number >x : Bar @@ -177,3 +201,26 @@ function test2(x: Bar) { >0 : number } +class Base { +>Base : Base + + a?: number; +>a : number | undefined + + f?(): number; +>f : (() => number) | undefined +} + +class Derived extends Base { +>Derived : Derived +>Base : Base + + a = 1; +>a : number +>1 : number + + f(): number { return 1; } +>f : () => number +>1 : number +} + diff --git a/tests/cases/conformance/types/namedTypes/optionalMethods.ts b/tests/cases/conformance/types/namedTypes/optionalMethods.ts index 47cd72fbae7..932521425f9 100644 --- a/tests/cases/conformance/types/namedTypes/optionalMethods.ts +++ b/tests/cases/conformance/types/namedTypes/optionalMethods.ts @@ -1,4 +1,5 @@ // @strictNullChecks: true +// @declaration: true interface Foo { a: number; @@ -20,6 +21,8 @@ function test1(x: Foo) { class Bar { a: number; b?: number; + c? = 2; + constructor(public d?: number, public e = 10) {} f() { return 1; } @@ -32,6 +35,9 @@ class Bar { function test2(x: Bar) { x.a; x.b; + x.c; + x.d; + x.e; x.f; x.g; let f1 = x.f(); @@ -40,3 +46,13 @@ function test2(x: Bar) { let h1 = x.h && x.h(); let h2 = x.h ? x.h() : 0; } + +class Base { + a?: number; + f?(): number; +} + +class Derived extends Base { + a = 1; + f(): number { return 1; } +}