tsover

Defining Overloads

How to define overloaded operators in tsover

Prior to following this guide, make sure you have setup tsover as described in Getting Started. To define operator overloads, you need to install a minimal runtime dependency called tsover-runtime.

npm install tsover-runtime

API

tsover-runtime exports the following:

  • Operator.plus - symbol used to define the + and += operators
  • Operator.minus - symbol used to define the - and -= operators
  • Operator.star - symbol used to define the * and *= operators
  • Operator.slash - symbol used to define the / and /= operators
  • Operator.deferOperation - symbol used to defer the operation to the other operand

Class Example

import {  } from 'tsover-runtime';

class  {
  : number;
  : number;

  constructor(: number, : number) {
    this. = ;
    this. = ;
  }

  // lhs + rhs
  [.](: , : ):  {
    return new (. + ., . + .);
  }

  [.](:  | number, :  | number): ;
  [.](
    :  | number,
    :  | number,
  ):  | typeof . {
    if (typeof  === 'number' &&  instanceof ) {
      return new ( * .,  * .);
    }
    if (typeof  === 'number' &&  instanceof ) {
      return new (. * , . * );
    }
    if ( instanceof  &&  instanceof ) {
      return new (. * ., . * .);
    }
    // Letting the other operand handle the operation
    return .;
  }
}

function () {
  'use tsover';
  const  = new (1, 2);

  const b =  * 2;
const b: Vec2f
const c = 4 * ;
const c: Vec2f
}

On this page