2

I want to use big.js or decimal.js to perform accurate calculations of decimal numbers in my basic Svelte app. To test it, I have imported the module in my App.svelte file:

import Decimal from "../node_modules/decimal.js/decimal.mjs";
import Big from "../node_modules/big.js/big.mjs";

However, when I create a Big object and use its built-in functions, I get an error in my IDE (Visual Studio Code), but the code works as expected in the console:

let myNumber = 3.2;
let secondNumber = new Big(myNumber).div(2);

Error: Property 'div' does not exist on type 'Big'

How can I get my IDE to recognise imported objects from modules? To clarify, I am not using typescript.

Sidders
  • 124
  • 10
  • What do you see if you log out `new Big(newNumber)`. `console.log('big', new Big(newNumber))` – Y. Gherbi May 28 '23 at 21:37
  • I just realised 'newNumber' should be 'myNumber' (I've amended my question). However, this is what I see in the console if I put `console.log("big", new Big(myNumber));` `big Big {s: 1, e: 0, c: Array(2), constructor: ƒ}` – Sidders May 28 '23 at 21:43
  • could it be related to this? `import Big from "../node_modules/big.js/big.mjs";` as far as I know you should just import modules like: `import Big from "big.js";` or like `import Big from "big.js/big.mjs";` Could you try it out? – Y. Gherbi May 28 '23 at 23:25
  • Just tried it out. If I do that, my IDE cannot locate the module files and they stop working altogether. It's odd, big.js and decimal.js still works in my project as-is, but the IDE keeps throwing the mentioned error because it doesn't seem to recognise them. I appreciate you coming up with possible solutions though! It a hard one. – Sidders May 29 '23 at 04:03
  • @Slidder So just to be sure, the error only shows up in your IDE, but works when you run the actual app in the browser? – Y. Gherbi May 29 '23 at 05:34
  • Yes, exactly that. And I want to fix it because the error messages clutter the project and are obscuring genuine errors. – Sidders May 29 '23 at 07:00
  • What IDE or code editor are you using? Mac/linux/pc? If you share a minimal version of the app that creates the error you are talking about, I am willing to take a look at it. I know how annoying those red squiggly lines can be – Y. Gherbi May 29 '23 at 08:15
  • Sure! Windows PC, using Visual Studio Code ver 1.70.2. Is that enough information? – Sidders May 31 '23 at 03:39
  • Could you share a repo to the app with this issue? Or maybe a link to an app with the minimal code to reproduce this? – Y. Gherbi Jun 01 '23 at 23:13
  • Here you go, but it would need to be copied into the IDE and the decimal.js module installed locally. Unfortunately, I don't know how (or if) a module can be imported using a URL only: https://svelte.dev/repl/11a837890af44b11860922662d0195bc?version=3.59.1 Thanks again. – Sidders Jun 04 '23 at 06:45
  • Did you install the Svelte plugin for VSCode? – Y. Gherbi Jun 05 '23 at 21:07
  • Maybe this will help: https://stackoverflow.com/questions/69645080/how-to-enable-type-checking-of-mjs-files-in-vs-code – Y. Gherbi Jun 05 '23 at 21:08

0 Answers0