josdejong / mathjs

An extensive math library for JavaScript and Node.js
https://mathjs.org
Apache License 2.0
14.44k stars 1.24k forks source link

Property 'Unit' does not exist on type 'MathJsStatic'. Did you mean 'unit'? error in Typescript #2286

Open HendrikPetertje opened 3 years ago

HendrikPetertje commented 3 years ago

When creating the following code in Typescript

import { MathJsStatic, create, all, MathType } from 'mathjs';
const math: MathJsStatic = create(all, {}) as MathJsStatic;

math.createUnit('cells'); // works perfectly
math.Unit; // results in: Property 'Unit' does not exist on type 'MathJsStatic'. Did you mean 'unit'?

It looks like Unit isn't properly part of the new MathJS typescript definitions or it's not linking correctly.

if I replace the line above with window['testing'] = math then the Unit is in fact present.

image

Edit: I am running mathjs 9.4.4 without @types/mathjs (relying on the built-in type definitions)

josdejong commented 3 years ago

Thanks for reporting!

Anyone able to help out adding type definitions for Unit?

janniklas-jakob commented 2 years ago

As described in this discussion, I cannot retrieve math.Unit.UNITS in typescript.

However, the following code works:

import { all, create } from "mathjs";
const math = create(all);
// @ts-ignore
console.log(math.Unit.UNITS);

Is this still being worked on?

josdejong commented 2 years ago

Thanks for reporting Jakob. There is no one working on that right now, help would be welcome.

And indeed, you can resort to @ts-ignore as a workaround.