roblox-ts
roblox-ts copied to clipboard
Custom Math Prototype
trafficstars
https://github.com/roblox-ts/compiler-types/pull/418 https://github.com/roblox-ts/types/pull/1172
Added these types to @rbxts/compiler-types
type Add<T> = { add: T };
type Sub<T> = { sub: T };
type Mul<T> = { mul: T };
type Div<T> = { div: T };
type IDiv<T> = { idiv: T };
type Concat<T> = { concat: T };
type Mod<T> = { mod: T };
type Pow<T> = { pow: T };
type Eq<T> = { eq: T };
type Lt<T> = { lt: T };
type Le<T> = { le: T };
type Gt<T> = { gt: T };
type Ge<T> = { ge: T };
type Unm<T> = { unm: T };
type Len<T> = { len: T };
TODO: Can we put these behind a namespace to reduce global type pollution?
Then defined CFrame in @rbxts/types as:
interface CFrame extends
Add<{
(this: CFrame, v3: Vector3): CFrame;
}>,
Sub<{
(this: CFrame, v3: Vector3): CFrame;
}>,
Mul<{
(this: CFrame, cframe: CFrame): CFrame;
(this: CFrame, v3: Vector3): CFrame;
(this: CFrame, other: CFrame | Vector3): CFrame | Vector3;
}>
{
// definition here
}
Examples:
interface MyType
extends Concat<{
(this: MyType, other: number): string;
}> {}
declare const a: CFrame;
declare const b: Vector3;
const c = a.add(b); // a + b
declare const foo: MyType;
const x = foo.concat(123); // foo .. 123
Any chance we could also get additional math overloads? I'm trying to use an existing BigNum lua library that also uses __mod, __pow, __lt, __gt, and __eq overloads and it would be great to be able to have ts compile const y = bigNum.pow(x); into local y = bigNum ^ x etc.