-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Operator overloading and primitive type declarations #42218
Comments
Alternatively, TypeScript should handle the |
@ExE-Boss That is a possibility, but there are three problems that I can see with that approach, the first being that the proposal that I mentioned does not use them, so TS would still need another form of exposing overloads, should it ever reach stage 4, the second being that one may not want the |
If js/ts support operator overloading, a lot of features would be possible, some code could be rewritten simplify. And some missing lib like python's Looking forward to have this promising features as soon as possible. |
@liudonghua123 I don't think that you understand what I'm proposing here? I'm merely asking for typing for this idea, your comment would be more suited to the ES proposal that I had linked, as they are proposing a real runtime feature. |
Note: This feature is being enabled through babel, and unfortunately doesn't really have any typescript support. Using an overloaded operator will show an error and be typed as "any". If ecmascript ever support operator overloading, then typescript will follow suit and these issues can be resolved. Here's the current proposal for how that could look like, although it's a long way's off from being accepted, if it ever is: https://github.com/tc39/proposal-operator-overloading Alternatively, there's a proposal for declaring that certain types have operator overloads, which would also work just perfectly: microsoft/TypeScript#42218 In the meantime, the errors will unfortunately remain present, although they won't cause any issues in production. BTW, the rhs can be any DecimalSource, but the lhs has to be a Decimal.
@crimsoncodes0 I think the syntax should look more like the c++ syntax type Position = {
x: number;
y: number;
};
function operator+(rhs: Position, lfs: number): Position {
return {
x: position.x + nbr,
y: position.y + nbr
}
}
let position1: Position = {
x: 1,
y: 2
};
let position2: Position = position1 + 2;
// result: { x: 3, y: 4 }
// Under the hood it would look more like this
let position2: Position = operator+(position1, 2); Under the hood, the compiler will create a unique function name for the addition operator function. The addition operator function should always return a value that is the same type as rhs argument. The addition operator function can also be used for the addition assignment operator if all the operator functions will return a new value. It would be more like a type of extension function. I think personally we should having operator overloading part of a class, it would make it messy and also the benefit of having them as functions they can be declared locally to a specific scope. There should be predefined interfaces for every operator that can be overloaded so they can be targeted in generic function, for example: function sum<T extends IAdtionOpertor>(items: T[]): T {
let [sum, ...restItems] = items;
for (const item of restItems) {
sum += item;
}
return sum;
}
let positions: Position[] = [
{x: 1, y: 1},
{x: 2, y: -1},
{x: 3, y: 3},
];
let positionSum = sum(positions);
// results: { x: 6, y: 3}; |
@joseDaKing Sorry, you seem to lack context, and haven't read my issue at all... I'm asking for operator overloading declarations, just like type declarations, they're not implementations. Note that I'm aiming to follow these points:
You ask for function operator+(rhs: Position, lfs: number): Position {
return {
x: position.x + nbr,
y: position.y + nbr
}
} Yet, I explicitly said,
Please, read the entire issue; if you had, you would've seen that I mentioned how ECMAScript itself is already implementing this: https://github.com/tc39/proposal-operator-overloading. |
So this really isn't a thing? I found these old issues closed by MS, so I thought maybe it was already added; this guy has a fairly complete description of the functions and rules... This would be anice feature to have been added to Typescript which has the type information required.... adding to JS is a lot of overhead in the javascript engine I'd think... |
I really think the operator overloading should be detached from the "primitive" proposal, since "primitives" can be implemented using, e.g., branded types. A more conservative operator overload syntax will perhaps be something like: declare operator "+"(lhs: Foo, rhs: Foo): Foo; Some of my (current) use cases include:
|
Work with packages like bignumber.js and dinero.js would improve significantly. |
A recent example from one of my projects; These types of "custom primitives" help type check mixing units like seconds / milliseconds or degrees/radians etc. type milliseconds = number & { readonly unit: unique symbol };
type seconds = number & { readonly unit: unique symbol };
declare global {
interface Date {
getTime(): milliseconds;
}
interface DateConstructor {
now(): milliseconds;
}
}
export function msToS(ms: milliseconds): seconds {
return (ms / 1000) as seconds;
}
export function sToMs(s: seconds): milliseconds {
return (s * 1000) as milliseconds;
}
const time = Date.now(); //milliseconds
console.log(msToS(time)); // OK
// console.log(sToMs(time)); // Argument of type 'milliseconds' is not assignable to parameter of type 'seconds'.
const delta = time - Date.now(); // milliseconds - milliseconds => number rather than milliseconds
console.log(msToS(delta)); // Argument of type 'number' is not assignable to parameter of type 'milliseconds'. It would be handy to declare that Additionally, it would be nice to allow implicit casts from these types to type milliseconds = number & { readonly unit?: unique symbol };
type seconds = number & { readonly unit?: unique symbol };
const foo = 1 as seconds;
msToS(foo); //error
msToS(foo + 0); //ok Being able to declare that |
Suggestion
🔍 Search Terms
Operators, operator types, operator overloading
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
Introduce a way to show what types can/cannot be gained from using an operator on a value of a type.
No, I am not asking for operations to be replaced with functions or such, like many other issues have asked for, I am merely asking for a way to show what types that an operation will yield when performed between two types.
Currently, there is no way to describe operator types in TS.
📃 Motivating Example
I don't have an idea for the syntax, but I'll introduce a partial syntax to showcase the idea here.
Let's imagine that TS allowed us to declare primitive types via, say, a
primitive
keyword:This is already valid TS:
This suggests that TS already has the notion of overloaded operators that I have suggested.
Note that operators may never have a body, as TSC is not allowed to emit runtime code for the operations.
Now, TS doesn't presently allow us to declare primitives, but operations between objects always throw the error:
And generally, yes, that is a good thing, but is it always?
Take this example:
I know that the ES abstract
ToPrimitive
will be called on both of these objects, resulting in the primitive string value contained within.Run the code yourself, it will result in
"foobar"
, and we know this, so let's tell TSC that too!But, instead of the type
string
being the result of the concatenation operator, we get this:Let's say that we were using a type that becomes a number, ex:
WebAssembly.Global
:That string example could could now be something like this:
Of course, as with anything else, this can be misused, but it's no worse than the already existing
2 + ""
semantics.💻 Use Cases
This can likely solve issues such as #28682 solely via user-implemented types!
If we could declare opaque types, such as those mentioned in #15408 or #40075, one could do stuff like, say, creating a NaN type, and stricter number types, all without runtime overhead and erasable types.
Toss in throw types and we can get some good error messages out of it too:
Another use, working with pointers into, say, WebAssembly memory, it usually makes no sense to do something like raising it to an exponent, and this could allow us to scope what operations are permitted.
Before:
after:
There will have to be some TSC enforced rules in order for it to actually be useful, ex: the return type of + must extend
number | string | bigint
, because nothing else could be possible.If the primitive type idea is too radical, it could be completely decoupled from the operator overloading, so that I may perform my object arithmetic with the safety of TS. :)
Also, eventually, TS may have to implement this anyways: https://github.com/tc39/proposal-operator-overloading
The text was updated successfully, but these errors were encountered: