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 — The result object is not typed as ResultSet in TypeScript · Issue #3469 · josdejong/mathjs

The result object is not typed as ResultSet in TypeScript #3469

New issue
Copy link
New issue
Copy link
Closed
Closed
The result object is not typed as ResultSet in TypeScript#3469
Copy link
Labels
buggood first issueRelatively localized/straightforward changes that would provide a good introduction to mathjs codehelp wantedtypescript

Description

@lucafaggianelli
lucafaggianelli
opened on Apr 29, 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
evaluate() returns a result object that could a primitive or a ResultSet, though the ResultSet type is not exported from the mathjs package nor the type is inferred from isResultSet.

Moreover I'd add some reference in the docs explaining that if formula is composed of multiple lines or multiple expressions, the returned result is not the last result but rather all the results as a ResultSet.

To Reproduce
Steps to reproduce the behavior.

Consider the snippet below where I try to get the result of a user-provided formula, all is working correctly but result is not typed

const result = evaluate(field.impactsFormula, scope)

    if (typeof result === 'number') {
      fieldData = result
    } else if (
      isResultSet(result) &&
      typeof result.valueOf().at(-1) === 'number'
    ) {
      // When there are multiple expressions, even just var assignments, mathjs returns
      // an array of results, so let's consider the last one as the right result
      fieldData = result.valueOf().at(-1)
    } else {
      return {
        success: false,
        error: `Formula "${field.impactsFormula}" did not return a number: ${result}`,
      }
    }

Activity

  1. josdejong commented on May 7, 2025

    @josdejong
    josdejong
    on May 7, 2025
    Owner
    More actions

    Thanks for reporting Luca!

    Anyone able to implement the interface for ResultSet in index.d.ts, use it as return type for isResultSet, and write a test in testTypes.ts? Help would be welcome.

  2. josdejong
    added
    bug
    help wanted
    typescript
    good first issueRelatively localized/straightforward changes that would provide a good introduction to mathjs code
    on May 7, 2025
  3. ranidam commented on May 18, 2025

    @ranidam
    ranidam
    on May 18, 2025
    Contributor
    More actions

    To improve TypeScript support when using evaluate() with multiple expressions, here’s a minimal fix:

    index.d.ts Changes

    1. Export ResultSet Interface
    export interface ResultSet {
      valueOf(): any[];
    }
    1. Update isResultSet Return Type
    export function isResultSet(value: any): value is ResultSet;
    1. test/types/testTypes.ts – Type Test
     import { evaluate, isResultSet, ResultSet } from 'mathjs';
    
    const result = evaluate('a = 5; b = 10; a + b');
    
    if (isResultSet(result)) {
      const values = result.valueOf();
      const last: number = values.at(-1);
      console.log(last); // Should output 15
    }
  4. josdejong commented on May 21, 2025

    @josdejong
    josdejong
    on May 21, 2025
    Owner
    More actions

    Thanks for sharing @ranidam . Would you be able to put together a PR with a fix?

  5. ranidam commented on May 26, 2025

    @ranidam
    ranidam
    on May 26, 2025
    Contributor
    More actions

    Thanks for the response! Yes, I’d happily put together a PR with the fix. I’ll include the ResultSet interface in index.d.ts, update the return type for isResultSet, and add a test case in test/types/testTypes.ts to verify the typing.

    Let me know if you’d like the ResultSet interface documented anywhere else or if there’s a preferred format for type tests. I’ll get the PR ready shortly.

  6. josdejong commented on May 26, 2025

    @josdejong
    josdejong
    on May 26, 2025
    Owner
    More actions

    Thanks! There is no need for additional documentation, just the definition of the interface itself and a test case.

  7. ranidam commented on May 26, 2025

    @ranidam
    ranidam
    on May 26, 2025
    Contributor
    More actions

    Perfect, thanks for confirming! I’ll proceed with the interface definition and the test case and open the PR shortly.

    Reacted by Jos de Jong
  8. josdejong
    closed this as completedin 823d13eon May 30, 2025
  9. lucafaggianelli commented on May 30, 2025

    @lucafaggianelli
    lucafaggianelli
    on May 30, 2025
    Author
    More actions

    Thanks ❤

  10. josdejong
    added a commit that references this issue on May 30, 2025

    fix: josdejong#3469 add `ResultSet` interface and improve `isResultSe…

    This issue will close once commit 823d13e is merged into the 'develop' branch.
    823d13e
  11. josdejong commented on May 30, 2025

    @josdejong
    josdejong
    on May 30, 2025
    Owner
    More actions

    This is fixed now in v14.5.2.

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

    buggood first issueRelatively localized/straightforward changes that would provide a good introduction to mathjs codehelp wantedtypescript

    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.