Improve operator overload typings (#480)

This commit is contained in:
Max Graey 2019-02-21 01:23:46 +02:00 committed by Daniel Wirtz
parent 951b6f9f45
commit 2c365ada5b
2 changed files with 40 additions and 34 deletions

View File

@ -931,7 +931,10 @@ declare function global(
): TypedPropertyDescriptor<any> | void;
/** Annotates a method as a binary operator overload for the specified `token`. */
declare function operator(token: string): (
declare function operator(token:
"[]" | "[]=" | "{}" | "{}=" | "==" | "!=" | ">" | "<" | "<=" | ">=" |
">>" | ">>>" | "<<" | "&" | "|" | "^" | "+" | "-" | "*" | "**" | "/" | "%"
): (
target: any,
propertyKey: string,
descriptor: TypedPropertyDescriptor<any>
@ -939,19 +942,22 @@ declare function operator(token: string): (
declare namespace operator {
/** Annotates a method as a binary operator overload for the specified `token`. */
export function binary(token: string): (
export function binary(token:
"[]" | "[]=" | "{}" | "{}=" | "==" | "!=" | ">" | "<" | "<=" | ">=" |
">>" | ">>>" | "<<" | "&" | "|" | "^" | "+" | "-" | "*" | "**" | "/" | "%"
): (
target: any,
propertyKey: string,
descriptor: TypedPropertyDescriptor<any>
) => TypedPropertyDescriptor<any> | void;
/** Annotates a method as an unary prefix operator overload for the specified `token`. */
export function prefix(token: string): (
export function prefix(token: "!" | "~" | "+" | "-" | "++" | "--"): (
target: any,
propertyKey: string,
descriptor: TypedPropertyDescriptor<any>
) => TypedPropertyDescriptor<any> | void;
/** Annotates a method as an unary postfix operator overload for the specified `token`. */
export function postfix(token: string): (
export function postfix(token: "++" | "--"): (
target: any,
propertyKey: string,
descriptor: TypedPropertyDescriptor<any>

View File

@ -4,137 +4,137 @@ class Tester {
constructor(public x: i32, public y: i32) {
}
@operator('+')
@operator("+")
static add(a: Tester, b: Tester): Tester {
return new Tester(a.x + b.x, a.y + b.y);
}
@operator('-')
@operator("-")
static sub(a: Tester, b: Tester): Tester {
return new Tester(a.x - b.x, a.y - b.y);
}
@operator('*')
@operator("*")
static mul(a: Tester, b: Tester): Tester {
return new Tester(a.x * b.x, a.y * b.y);
}
@operator('/')
@operator("/")
static div(a: Tester, b: Tester): Tester {
return new Tester(a.x / b.x, a.y / b.y);
}
@operator('%')
@operator("%")
static mod(a: Tester, b: Tester): Tester {
return new Tester(a.x % b.x, a.y % b.y);
}
@operator('**')
@operator("**")
static pow(a: Tester, b: Tester): Tester {
return new Tester(<i32>(a.x ** b.x), <i32>(a.y ** b.y));
}
@operator('|')
@operator("|")
static or(a: Tester, b: Tester): Tester {
return new Tester(a.x | b.x, a.y | b.y);
}
@operator('&')
@operator("&")
static and(a: Tester, b: Tester): Tester {
return new Tester(a.x & b.x, a.y & b.y);
}
@operator('^')
@operator("^")
static xor(a: Tester, b: Tester): Tester {
return new Tester(a.x ^ b.x, a.y ^ b.y);
}
@operator('==')
@operator("==")
static equals(a: Tester, b: Tester): bool {
return a.x == b.x && a.y == b.y;
}
@operator('!=')
@operator("!=")
static notEquals(a: Tester, b: Tester): bool {
return a.x != b.x && a.y != b.y;
}
@operator('>')
@operator(">")
static greater(a: Tester, b: Tester): bool {
return a.x > b.x && a.y > b.y;
}
@operator('>=')
@operator(">=")
static greaterEquals(a: Tester, b: Tester): bool {
return a.x >= b.x && a.y >= b.y;
}
@operator('<')
@operator("<")
static less(a: Tester, b: Tester): bool {
return a.x < b.x && a.y < b.y;
}
@operator('<=')
@operator("<=")
static lessEquals(a: Tester, b: Tester): bool {
return a.x <= b.x && a.y <= b.y;
}
@operator('>>')
@operator(">>")
static shr(value: Tester, shift: i32): Tester {
return new Tester(value.x >> shift, value.y >> shift);
}
@operator('>>>')
@operator(">>>")
static shu(value: Tester, shift: i32): Tester {
return new Tester(value.x >>> shift, value.y >>> shift);
}
@operator('<<')
@operator("<<")
static shl(value: Tester, shift: i32): Tester {
return new Tester(value.x << shift, value.y << shift);
}
// unary opterators
@operator.prefix('~')
@operator.prefix("~")
static not(value: Tester): Tester {
return new Tester(~value.x, ~value.y);
}
@operator.prefix('!')
@operator.prefix("!")
static excl(value: Tester): bool {
return !value.x && !value.y;
}
@operator.prefix('+')
@operator.prefix("+")
static pos(value: Tester): Tester {
return new Tester(+value.x, +value.y);
}
@operator.prefix('-')
@operator.prefix("-")
static neg(value: Tester): Tester {
return new Tester(-value.x, -value.y);
}
@operator.prefix('++')
@operator.prefix("++")
inc(): this {
++this.x;
++this.y;
return this;
}
@operator.prefix('--')
@operator.prefix("--")
dec(): this {
--this.x;
--this.y;
return this;
}
@operator.postfix('++')
@operator.postfix("++")
postInc(): Tester {
return new Tester(this.x + 1, this.y + 1);
}
@operator.postfix('--')
@operator.postfix("--")
postDec(): Tester {
return new Tester(this.x - 1, this.y - 1);
}
@ -297,12 +297,12 @@ assert(incdec.x == 0 && incdec.y == 1);
class TesterInlineStatic {
constructor(public x: i32, public y: i32) {
}
@inline @operator('+')
@inline @operator("+")
static add(a: TesterInlineStatic, b: TesterInlineStatic): TesterInlineStatic {
return new TesterInlineStatic(a.x + b.x, a.y + b.y);
}
@inline @operator.postfix('++')
@inline @operator.postfix("++")
static postInc(a: TesterInlineStatic): TesterInlineStatic {
return new TesterInlineStatic(a.x + 1, a.y + 1);
}
@ -317,12 +317,12 @@ assert(ais.x == 4 && ais.y == 6);
class TesterInlineInstance {
constructor(public x: i32, public y: i32) {
}
@inline @operator('+')
@inline @operator("+")
add(b: TesterInlineInstance): TesterInlineInstance {
return new TesterInlineInstance(this.x + b.x, this.y + b.y);
}
@inline @operator.postfix('++')
@inline @operator.postfix("++")
postInc(): TesterInlineInstance {
return new TesterInlineInstance(this.x + 1, this.y + 1);
}