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-runtimeAPI
tsover-runtime exports the following:
Operator.plus- symbol used to define the+and+=operatorsOperator.minus- symbol used to define the-and-=operatorsOperator.star- symbol used to define the*and*=operatorsOperator.slash- symbol used to define the/and/=operatorsOperator.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 c = 4 * ;}