GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Typescript definitions for add and multiply incorrectly allow 0 and 1 arguments · Issue #3499 · josdejong/mathjs

Typescript definitions for add and multiply incorrectly allow 0 and 1 arguments #3499

New issue
Copy link
New issue
Copy link
Closed
Closed
Typescript definitions for add and multiply incorrectly allow 0 and 1 arguments#3499
Copy link
Labels
bug

Description

@mrft
mrft
opened on Jun 30, 2025
Issue body actions

The issues section is used only for bug reports. Please use the Discussions section to ask questions and share ideas and suggestions.

Describe the bug
both math.add() and math.multiply() produce a runtime error when called without any arguments, but the types do not complain about this.
Caused by these lines:

  add<T extends MathType>(...values: T[]): T
  add(...values: MathType[]): MathType

  multiply(...values: MathType[]): MathType

To Reproduce

echo 'import { add, multiply, subtract } from "mathjs";

try {
  console.log(add()); // No compile error, but runtime error
} catch(err) {
  console.error("add() FAILED");
}
try {
  console.log(add(1)); // No compile error, but runtime error
} catch(err) {
  console.error("add(1) FAILED");
}

try {
  console.log(multiply()); // No compile error, but runtime error
} catch(err) {
  console.error("multiply() FAILED");
}

try {
  console.log(multiply(2)); // No compile error, but runtime error
} catch(err) {
  console.error("multiply(2) FAILED");
}


subtract(); // This throws a TypeError as expected
' > math_test_add_and_multiply.ts


deno math_test_add_and_multiply.ts

Activity

  1. mrft
    changed the title [-]Typescript definitions for add and multiply incorrectly allow 0 arguments[/-] [+]Typescript definitions for add and multiply incorrectly allow 0 and 1 arguments[/+] on Jul 1, 2025
  2. gwhitney commented on Jul 1, 2025

    @gwhitney
    gwhitney
    on Jul 1, 2025
    Collaborator
    More actions

    It would seem to me that math.add(7) should simply return 7, somewhat analogous to the fact that +7 evaluates just to 7 and that the sum of one number is just that number; since we allow math.add(2,3,5) to get 10, for consistency math.add(2) should be 2. Under this convention, then math.add() should evaluate to 0.

    If we adopt that convention for add, then multiply of one argument should also be a no-op, and multiply of zero arguments should return 1.

    In other words, I am suggesting that the resolution here perhaps should be to define add and multiply for one and zero arguments, rather than changing the typescript definitions. Thoughts, @josdejong ? One issue with the proposal is that with no arguments, which 0 or 1 should mathjs return? The JavaScript built-in number type?

  3. josdejong commented on Jul 2, 2025

    @josdejong
    josdejong
    on Jul 2, 2025
    Owner
    More actions

    Thanks @mrft, good find & fix.

    Good idea Glen. I've moved that discussion into a separate issue #3504 to keep the current issue just about making the type definitions match the actual behavior.

  4. josdejong
    added
    bug
    on Jul 2, 2025
  5. josdejong
    added a commit that references this issue on Jul 2, 2025

    fix: #3493 #3499 #3494 type definitions of `unit(number)`, for `add` …

    09f6da9
  6. josdejong commented on Jul 2, 2025

    @josdejong
    josdejong
    on Jul 2, 2025
    Owner
    More actions

    Fixed now in v14.5.3

  7. josdejong
    closed this as completedon Jul 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402792 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.