roblox-ts icon indicating copy to clipboard operation
roblox-ts copied to clipboard

Custom Math Prototype

Open osyrisrblx opened this issue 3 years ago • 1 comments
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

osyrisrblx avatar Jun 18 '22 03:06 osyrisrblx

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.

BlankSourceCode avatar Feb 14 '24 05:02 BlankSourceCode