Skip to content

covariant subtype values with @template

concavelenz edited this page Nov 26, 2014 · 3 revisions

A couple of useful tricks using @template.

Return the calling subtype from a super class method:

/** @constructor */ function Foo() {}

/**

  • @this {THIS}
  • @return {THIS}
  • @template THIS */ Foo.prototype.method = function() { return this; }

/** @constructor @extends {Foo} */ function Bar() {} Bar.prototype = new Foo();

var x = new Bar().method(); // x is of type "Bar"

Clone this wiki locally