From e4d30515323724f3884de8f29e21a418473857ca Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 27 Jan 2015 22:39:48 -0800 Subject: [PATCH] Added Jason's example for contextually typing parameters. --- .../superCallParameterContextualTyping3.js | 67 +++++++++++++++++ .../superCallParameterContextualTyping3.types | 73 +++++++++++++++++++ .../superCallParameterContextualTyping3.ts | 31 ++++++++ 3 files changed, 171 insertions(+) create mode 100644 tests/baselines/reference/superCallParameterContextualTyping3.js create mode 100644 tests/baselines/reference/superCallParameterContextualTyping3.types create mode 100644 tests/cases/conformance/expressions/contextualTyping/superCallParameterContextualTyping3.ts diff --git a/tests/baselines/reference/superCallParameterContextualTyping3.js b/tests/baselines/reference/superCallParameterContextualTyping3.js new file mode 100644 index 00000000000..e33bcb29bb8 --- /dev/null +++ b/tests/baselines/reference/superCallParameterContextualTyping3.js @@ -0,0 +1,67 @@ +//// [superCallParameterContextualTyping3.ts] +interface ContextualType { + method(parameter: T): void; +} + +class CBase { + constructor(param: ContextualType) { + } + + foo(param: ContextualType) { + } +} + +class C extends CBase { + constructor() { + // Should be okay. + // 'p' should have type 'string'. + super({ + method(p) { + p.length; + } + }); + + // Should be okay. + // 'p' should have type 'string'. + super.foo({ + method(p) { + p.length; + } + }); + } +} + +//// [superCallParameterContextualTyping3.js] +var __extends = this.__extends || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + __.prototype = b.prototype; + d.prototype = new __(); +}; +var CBase = (function () { + function CBase(param) { + } + CBase.prototype.foo = function (param) { + }; + return CBase; +})(); +var C = (function (_super) { + __extends(C, _super); + function C() { + // Should be okay. + // 'p' should have type 'string'. + _super.call(this, { + method: function (p) { + p.length; + } + }); + // Should be okay. + // 'p' should have type 'string'. + _super.prototype.foo.call(this, { + method: function (p) { + p.length; + } + }); + } + return C; +})(CBase); diff --git a/tests/baselines/reference/superCallParameterContextualTyping3.types b/tests/baselines/reference/superCallParameterContextualTyping3.types new file mode 100644 index 00000000000..4a8515d110c --- /dev/null +++ b/tests/baselines/reference/superCallParameterContextualTyping3.types @@ -0,0 +1,73 @@ +=== tests/cases/conformance/expressions/contextualTyping/superCallParameterContextualTyping3.ts === +interface ContextualType { +>ContextualType : ContextualType +>T : T + + method(parameter: T): void; +>method : (parameter: T) => void +>parameter : T +>T : T +} + +class CBase { +>CBase : CBase +>T : T + + constructor(param: ContextualType) { +>param : ContextualType +>ContextualType : ContextualType +>T : T + } + + foo(param: ContextualType) { +>foo : (param: ContextualType) => void +>param : ContextualType +>ContextualType : ContextualType +>T : T + } +} + +class C extends CBase { +>C : C +>CBase : CBase + + constructor() { + // Should be okay. + // 'p' should have type 'string'. + super({ +>super({ method(p) { p.length; } }) : void +>super : typeof CBase +>{ method(p) { p.length; } } : { method(p: string): void; } + + method(p) { +>method : (p: string) => void +>p : string + + p.length; +>p.length : number +>p : string +>length : number + } + }); + + // Should be okay. + // 'p' should have type 'string'. + super.foo({ +>super.foo({ method(p) { p.length; } }) : void +>super.foo : (param: ContextualType) => void +>super : CBase +>foo : (param: ContextualType) => void +>{ method(p) { p.length; } } : { method(p: string): void; } + + method(p) { +>method : (p: string) => void +>p : string + + p.length; +>p.length : number +>p : string +>length : number + } + }); + } +} diff --git a/tests/cases/conformance/expressions/contextualTyping/superCallParameterContextualTyping3.ts b/tests/cases/conformance/expressions/contextualTyping/superCallParameterContextualTyping3.ts new file mode 100644 index 00000000000..760337f3efa --- /dev/null +++ b/tests/cases/conformance/expressions/contextualTyping/superCallParameterContextualTyping3.ts @@ -0,0 +1,31 @@ +interface ContextualType { + method(parameter: T): void; +} + +class CBase { + constructor(param: ContextualType) { + } + + foo(param: ContextualType) { + } +} + +class C extends CBase { + constructor() { + // Should be okay. + // 'p' should have type 'string'. + super({ + method(p) { + p.length; + } + }); + + // Should be okay. + // 'p' should have type 'string'. + super.foo({ + method(p) { + p.length; + } + }); + } +} \ No newline at end of file