Skip to content
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

typeOf() does not remain consistent with typed-function types #3376

Open
gwhitney opened this issue Feb 1, 2025 · 1 comment
Open

typeOf() does not remain consistent with typed-function types #3376

gwhitney opened this issue Feb 1, 2025 · 1 comment

Comments

@gwhitney
Copy link
Collaborator

gwhitney commented Feb 1, 2025

Describe the bug
The typed-function typing system is centrally used, for example in defining and extending most of the methods mathjs provides. However, the typeOf method that mathjs provides bypasses the typed-function type system altogether, trying to determine the type of the object in its own way that is not necessarily consistent with typed-function's notion of the type of the entity.

To Reproduce
There are many possibly ways this can come up, but the simplest is by adding a typed-function type with a non-standard type test. Here's a toy example:

import {all, create} from 'mathjs'
const math = create(all)

math.typed.addTypes([{
    name: 'Negative',
    test: x => typeof x === 'number' && x < 0
}], 'number') // specify the new types are added _before_ number in the hierarchy

math.import({
    sqRt: math.typed('sqRt', {
        Negative: () => 'Sorry charlie',
        number: x => Math.sqrt(x)
    })
})

console.log(math.evaluate('[sqRt(-4), sqRt(4), typeOf(-4)].valueOf()'))

This prints [ 'Sorry charlie', 2, 'number' ] showing that the type definition worked and that functions can use it but that mathjs does not realize that -4 is now of type Negative. While this example is a bit frivolous, it can come up in more reasonable contexts: structured plain object types that mathjs then identifies as just Object when I want to know that they are a Polar object, or that sort of thing.

Recommendation
typed-function has an unexported method findTypeNames that determines all of the matching types for an entity. typed-function should be updated to export this method, and mathjs typeOf should simply return the first (most specific) element of this list.

@gwhitney
Copy link
Collaborator Author

gwhitney commented Feb 1, 2025

Workaround:

math.import({
    sqRt: math.typed('sqRt', {
        Negative: () => 'Sorry charlie',
        number: x => Math.sqrt(x)
    }),
    typeOf: math.typed('typeOf', {Negative: () => 'Negative'})
})

This redundant specification of the typeOf the new type of entity should not be necessary, but in the meantime, at least it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant