mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

Adds Unit constructor to fix type definitions

Open justinfranco opened this issue 3 years ago • 5 comments

Fixes #2286 by updating MathJsStatic in the type definitions to include a constructor for the Unit interface.

justinfranco avatar Sep 05 '22 07:09 justinfranco

I was having the same problem described in issue #2286. When trying to define a type for a prop in my typescript project as math.Unit I would get the error Property 'Unit' does not exist on type 'MathJsStatic'. Did you mean 'unit'?.

Adding a constructor to MathJsStatic for the Unit interface seems to fix #2286.

Please let me know if there is a different/preferred to fix the type definition for Unit or if you have any feedback.

justinfranco avatar Sep 05 '22 07:09 justinfranco

Thanks Justin!

I'm a bit in doubt on whether this is the right approach. There is already an interface Unit { ... } defined, with all the methods and properties that a Unit has. I guess we should change that into a class definition, and/or add a definition the constructor there? What do you think?

It would be nice if you can add a usage example to types/index.ts to verify that it indeed works to use Unit.

josdejong avatar Sep 06 '22 12:09 josdejong

@justinfranco did you see my comments of last week?

josdejong avatar Sep 12 '22 07:09 josdejong

Sorry, I updated my dependencies and and tsconfig this week and it is causing other issues with mathjs so I haven't been able to reproduce the issue. The previous way of importing mathjs using import { create, all } from 'mathjs' doesn't seem to work anymore and is showing the error File '/home/justin/cookbook/node_modules/mathjs/types/index.d.ts' is not a module.. The only way I have been able to get it to work is by using import math from "mathjs/lib/browser/math.js";. I am using esnext now in tsconfig for my module not sure if that has something to do with it.

Once I get this resolved I'm curious to see if I still get the same problem that I originally opened this PR for. If you have any suggestions for the error please let me know. As for your comment, I think you are probably right that this change is probably not the optimal way to handle the problem. Once I figure out this newer problem I can look at potenitally changing it into a class definition.

justinfranco avatar Sep 12 '22 09:09 justinfranco

Thanks for the update, one step at a time 😅

The previous way of importing mathjs using import { create, all } from 'mathjs' doesn't seem to work anymore and is showing the error File '/home/justin/cookbook/node_modules/mathjs/types/index.d.ts' is not a module.. The only way I have been able to get it to work is by using import math from "mathjs/lib/browser/math.js";. I am using esnext now in tsconfig for my module not sure if that has something to do with it.

That sounds like a serious inconvenience to use the project in TypeScript. If you happen to figure out the cause and know how to improve the TS definitions please let us know (or create another PR for that).

josdejong avatar Sep 12 '22 13:09 josdejong

Just a quick update, I'm still looking at the not a module problem but I probably won't have time to do much work on this for at least a few weeks. Since I think we will end up using a different solution than originally proposed in this PR I will just close it for now to clean things up. Once I get a solution for either problem I will post a new PR hopefully sometime in October. Thanks for the feedback so far!

justinfranco avatar Sep 23 '22 05:09 justinfranco

👍 thanks for reporting back.

josdejong avatar Sep 23 '22 13:09 josdejong