Skip to content

this in types — the this parameter and polymorphic this

Level: 301 · for anyone who has chained method calls

Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.

One line: A method whose return type is this returns the subclass, so new HtmlBuilder().add("x").bold() type-checks where a : Builder return type loses bold; a this: parameter makes tsc reject a bare greet() call.

Keywords: this type, polymorphic this, this parameter

What the finished page will answer

  • Why does new HtmlBuilder().add("x").bold() type-check when add returns this, and fail when it returns Builder?
  • What does a this: parameter declare, and what is left of it after Node strips the types?
  • What does TS2684 say for a bare greet(), and what do person.greet() and greet.call(person) change?
  • Does tsc catch setTimeout(g.greet, 0) when greet declares this: Greeter, and what does Node print when the timer fires?
  • How does a method returning this is Dog narrow the object it was called on?

Examples it will need

  • this_types_builder_tserror.ts — the TS2339 error when a Builder-typed return loses bold, and the TS2684 error for a bare greet() call
  • this_types_fluent_ts.ts — the HTML an HtmlBuilder chain produces, each call returning the subclass
  • this_types_lost_this_sh.sh — tsc's silence for setTimeout(g.greet), then what the method prints when Node calls it with the wrong this

See also

Sources to start from