Convert mathjs to Typescript #2742
Replies: 25 comments 2 replies
-
I've been looking through the issues to determine the current state of TypeScript support. I see pointers to the @types/mathjs definitions on DefinitelyTyped, but the latest version of mathjs with type definitions is major version 6, 2 major versions behind and nearly a year out of date. I agree with @husayt, an implementation in TypeScript would provide numerous benefits:
Relevant issue re out-of-date DefinitelyTyped definitions: #1539 |
Beta Was this translation helpful? Give feedback.
-
Thanks for your suggestion @husayt. I'm positive about looking into converting the code base to TypeScript. I expect that this will not be easy. There is quite some special stuff going on with dynamically creating functions. mathjs has these typed-functions which define (runtime) types for example, and generates index files based on factory functions etc. I can imagine getting all types right can be hard, and may become verbose, forcing you to write types twice and ending up with unwieldy generics to make stuff work. Maybe we want/need to make changes in the architecture to make mathjs play nice with TypeScript. I think it would be good to first investigate what is required to get TypeScript into the code base, and then decide on if and to what extend we would like to integrate TypeScript. TypeScript can add value, but it also comes at a cost. A pragmatic short term solution of course is to get the type definitions of @types/mathjs on par. Anyone interested in picking this up? |
Beta Was this translation helpful? Give feedback.
-
@josdejong I am happy to start with this. Yes, it will take time to switch fully to Typescript. But the good thing with TS is that this can be done gradually and without breaking everything. As JS is TS we can start with very loose tsconfig and then try to introduce types, and as we are we are doing that we can switch to more strict settings. This will happen without breaking things. If there are any breaking changes required we can leave them to the end. Then we can decide if they should come in v9.x or not. This way most of the changes can be done as part of v8 i.e. nonbreaking changes. If you are happy with this approach I can do the first step. |
Beta Was this translation helpful? Give feedback.
-
If I may chime in, there is value in not needing a build script. It looks like we do still use build scripts to convert the files in src to files in lib but if I understand correctly this is a temporary measure whilst we work out how to get ESM working properly in node. A nice feature of some javascript libraries that we should (I believe) aim to support is that a library can beimported "as is" and transpilation is a extra step if you need the library to run in an old browser (babel inserts shims for IE11 for example) or want to optimise (download size etc). |
Beta Was this translation helpful? Give feedback.
-
Thanks @husayt! Yes it's very nice that we can implementing TypeScript gradually. But let's first do an experiment to investigate all the bears we will encounter down the road and have a clear view on how the code base will look like in the end. Then make a clear plan, before we start for real. A first small, pragmatic step I think would be to improve the existing type definitions. That will yield a lot of value with very little effort. @harrysarson I think we cannot get rid of the build tools any time soon. Part of it is to build ESM, CommonJs, and bundle output, but an other part is to generate index files (see the |
Beta Was this translation helpful? Give feedback.
-
@josdejong looks like help is required here. if yall are moving forward with the convert lmk |
Beta Was this translation helpful? Give feedback.
-
@jeremylorino yes help would be very welcome. |
Beta Was this translation helpful? Give feedback.
-
@josdejong is there a project/branch/plan for this or has work not started? Don't want to redo work. |
Beta Was this translation helpful? Give feedback.
-
Thanks @jeremylorino for your offer. There is nothing yet in that direction. I think it will be a large undertaking. It could very well be that we need to make some changes in the architecture in case it doesn't play nice with TypeScript, so it would be good to first create a minimalistic proof of concept to figure out those issues. FYI: it's good to be aware of an other (early) experiment/idea regarding the architecture of the library discussed in #1975 which potentially would mean big changes in the architecture. |
Beta Was this translation helpful? Give feedback.
-
Continuation of the discussion here: #2582 (comment) In order to move We would like to see if we can tackle two challenges in one go though: the current architecture is relatively complex, and we miss TypeScript support. @gwhitney had some great ideas regarding the architecture and created a small POC named |
Beta Was this translation helpful? Give feedback.
-
Three major issues I see that would need to be explored right away in a proof-of-concept for Typescript along the lines Jos just suggested:
to be
to be
|
Beta Was this translation helpful? Give feedback.
-
@josdejong @gwhitney let me get caught up. It's been a while since I've looked at the code. I'll follow up soon. |
Beta Was this translation helpful? Give feedback.
-
No problem at all. Please keep us posted if you try out some experiments with this. It's a big undertaking. |
Beta Was this translation helpful? Give feedback.
-
Short answer, no. Something like this would have to take place TS Playground const fn1 = typed((n: number) => 'Hello');
fn1(1);
const typedConversion = typed.addConversion((s: string) => 0);
const fn2 = typedConversion(n => 'Hello');
fn2(1);
fn2("2");
fn2(false); // Argument of type 'boolean' is not assignable to parameter of type 'string | number'. I know this is only the first issue in the set you mentioned; yet it seemed like the most important. So now I have to ask, why do we need
That should help us move the needle. |
Beta Was this translation helpful? Give feedback.
-
@josdejong FYI typed-function#89 is pretty close. I pulled it down and got it to work on "Simple" lib; complex typings lol |
Beta Was this translation helpful? Give feedback.
-
Well, I am pretty sure we want runtime type dispatch, i.e. different behaviors for a function depending on its input type. That's not something typescript provides out of the box. It doesn't have to be a rewrite of typed-function that provides type dispatch, it could be another facility, but I feel like it's needed at the core of math.js/ts. I like your idea that adding a conversion returns a new function factory that types properly with the conversion, but don't quite see how the library can then offer up the latest greatest factory with all of the conversions added by any module... it seems like the charm of mathjs/typed-function right now is that conversions are added one place and then used elsewhere without the client having to worry about what conversions are in effect; and for customization, more can be added later. It feels like in TypeScript something in that model will have to be relinquished. But maybe there's a clever enough architecture to keep all those characteristics. |
Beta Was this translation helpful? Give feedback.
-
Ok that helps a lot. Let's start by defining what features
I need some examples in Typescript has some tricks up its sleeves but there are |
Beta Was this translation helpful? Give feedback.
-
Every operator of mathjs is defined "piecewise" by giving its behavior on different input types. The code for a complex input typically has nothing at all to do with the code for a real matrix input, say. This is a very valuable modularity in the design of mathjs. If I am defining the "absolute value" for a quaternion, for example, I don't even have to think about how it's computed for a bigint. But on the other hand, when I am using mathjs, I can simply call the |
Beta Was this translation helpful? Give feedback.
-
Well, again, t think this is more about how mathjs is used, not how it's implemented. For example, one of the things that attracted me to mathjs is that we will need a data type that represents (finite or infinite "to the right") sequences of whole numbers. We don't expect such a type to be built in to a JavaScript CAS, because its a bit specialized, but it's very attractive to have a CAS in which we can load up the base system, add our type and some operations on it, and then use entities of the new type alternating with pre-existing types, etc -- they all just work together. Is this helping any with clarifying things? Sure, it would be great if for my new sequence type the compiler can barf if I add a number to a sequence (which should add it elementwise) but then try to compute its multiplicative inverse (which would be defined for a number but doesn't make sense for a sequence). But it's more important that it be able to select and execute the right behavior for a scalar plus a sequence in the first place. |
Beta Was this translation helpful? Give feedback.
-
@gwhitney awesome!
This helps me determine my approach. |
Beta Was this translation helpful? Give feedback.
-
The discussion in josdejong/typed-function#89 is probably also relevant |
Beta Was this translation helpful? Give feedback.
-
Referencing josdejong/typed-function#123 here. There are some interesting POC's there, though we're not there yet. Much of the discussion of converting mathjs to Typescript boils down to first having full TypeScript support in |
Beta Was this translation helpful? Give feedback.
-
Note that the Pocomath proof of concept on offer for #1975 now allows return-type annotation, which should at least in some sense be a step closer to TypeScript. |
Beta Was this translation helpful? Give feedback.
-
Ah, I see: https://code.studioinfinity.org/glen/pocomath/commit/31add66f4cc1f5768c8e0697214dbcd624754bf0 That is nice, indeed one step closer towards TypeScript 😎 |
Beta Was this translation helpful? Give feedback.
-
Folks interested in this discussion should probably be aware of this other apparently completely independent effort to produce a TypeScript CAS: https://github.com/cortex-js/compute-engine. It seems quite new, and has a much less extensive list of implemented operations at the moment, and doesn't seem to state type extensibility as one of its explicit design goals, but still it's more of TypeScript CAS than mathjs is at the moment ;-) |
Beta Was this translation helpful? Give feedback.
-
Hi Jos,
Just wanted to ask your opinion regarding switching matjhs to Typescript? Obviously there will be a cost to this, but when it's done it will be a win for JS users as well as for TS users. Also considering the size of the library, that will help to grow it further with minimal errors, as TS helps weeding out lots of nasty bugs.
If this is something you feel positive about, I will be happy to help.
Beta Was this translation helpful? Give feedback.
All reactions