Type conversion precedence #2885
-
I'm trying to be able to do both:
I can get 1 working or 2 working but not both because the string concatenation is taking precedence over the color plus operator. Is there a way to specify which order newly added type operators will be tested/applied? Changing the order they are imported does not seem to have an affect. Some code: const addString = mathjs.typed('add', {
'string, string': function (a: string, b: string) {
return a + b
}
})
mathjs.import({ addString })
mathjs.typed.addType({
name: 'Color',
test: function (maybeColor: string) {
return maybeColor.startsWith("#")
},
})
const addColor = mathjs.typed('add', {
'Color, Color': function (a: string, b: string) {
// some logic to add colors, as a test though we could just remove the `#` from the start of the second color.
return a + b.slice(1)
}
})
mathjs.import({ addColor }) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You'll indeed have to have the type check for |
Beta Was this translation helpful? Give feedback.
You'll indeed have to have the type check for
Color
before testingstring
, because all colors are also strings. Looking at the API oftyped-function
, I see thetyped.addType
function doesn't allow this kind of ordering, but I think you can usetyped.addTypes(types, before)
where you can specifybefore
as"string"
.