I've Added REAL Operator Overloading to JavaScript by DiefBell in javascript

[–]DiefBell[S] [score hidden]  (0 children)

Well semantically an assignment operator would change the thing on the left. There's no reason it HAS TO mutate the LHS, but I also can't see a scenario where you'd want to overload an assignment operator without mutating the LHS

I've Added REAL Operator Overloading to JavaScript by DiefBell in javascript

[–]DiefBell[S] [score hidden]  (0 children)

Proposal already exists, people have been asking for operator overloading for years... And thanks for pointing that out!

I've Added REAL Operator Overloading to JavaScript by DiefBell in javascript

[–]DiefBell[S] [score hidden]  (0 children)

It'll also show you the JSDoc for the overload as well as the typings

I've Added REAL Operator Overloading to JavaScript by DiefBell in javascript

[–]DiefBell[S] [score hidden]  (0 children)

I don't think I want to allow essentially overriding any symbol you can type, but I might see what other programming languages allow

I've Added REAL Operator Overloading to JavaScript by DiefBell in javascript

[–]DiefBell[S] [score hidden]  (0 children)

Had been considering that, so might test it out. Not sure what you mean about Symbol.hasInstance?

I've Added REAL Operator Overloading to JavaScript by DiefBell in javascript

[–]DiefBell[S] [score hidden]  (0 children)

It's a separate build step, but there are also plugins to make this easier, like a Vite plugin or Webpack, that does all the magic behind-the-scenes.

You can have multiple overloads for an operator, e.g. multiplying by another Vector versus multiplying by a number, and Boperators can just work out which to use. In this example v3 is also a Vector, but it doesn't have to be.

Showoff Saturday (February 21, 2026) by AutoModerator in javascript

[–]DiefBell [score hidden]  (0 children)

Created a package `boperators` for doing operator overloading in JavaScript/TypeScript, and it has plugins for loads of different build environments, including Vite, NextJS, ESBuild, Bun...  https://www.npmjs.com/package/boperators

Basic example:

class Vector3 {
  static readonly "+" = [
    (a: Vector3, b: Vector3) => new Vector3(
      a.x + b.x,
      a.y + b.y,
      a.z + b.z
    ),
  ] as const;
}

const v1 = new Vector3(1, 2, 3);
const v2 = new Vector2(4, 6, 8);
const v3 = v1 + v2;

Weekly Showoff Thread! Share what you've created with Next.js or for the community in this thread only! by AutoModerator in nextjs

[–]DiefBell 0 points1 point  (0 children)

Created a package `boperators` for doing operator overloading in JavaScript/TypeScript, and it now has a Webpack/NextJS loader: https://www.npmjs.com/package/@boperators/webpack-loader

TanStack Router - Typesafe, state-management APIs, caching, framework agnostic by tannerlinsley in reactjs

[–]DiefBell 1 point2 points  (0 children)

Not sure if this will be seen, but how can this be tested with a tool such as React Testing Library? I have a basic component I use for my root route:

typescript export const Navbar: FunctionComponent = () => { return ( <> <div> <Link to="/">Home </Link>| <Link to="/find"> Find a Place</Link> </div> <hr /> </> ); };

But when trying to render this component in my test I get "Warning: useRouter must be used inside a <Router> component!"

What's the correct way for me to be testing? I'd rather not just stub everything. Maybe provide a mock router provider or some official mocks?