mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

Improve on TypeScript definitions to relfect support for multiple data types

Open dmitryrn opened this issue 6 years ago • 8 comments

Can't one create a matrix of custom elements, e.g. objects, like {type:'something'}? I noticed that only numbers are supported

dmitryrn avatar Apr 03 '20 22:04 dmitryrn

Matrices can contain elements of any type, even mixed together. Can you explain what you mean exactly?

josdejong avatar Apr 04 '20 12:04 josdejong

I use typescript and mathjs does not allow me to use anything else than number[][]. Same for .map and .forEach.

image

Here I can see that data param is declared as Matrix | MathArray but I do not know how to use Matrix (is it something internal?). In examples I saw only any[][] usage. https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/mathjs/index.d.ts#L239

Here I try to use .map on matrix, type definitions do not allow me to do it: image Typescript think that I returning Unit here because I returning an object (https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/mathjs/index.d.ts#L30)

Seems one can do const map = mathjs.map(zeros2d, (val, [x, y]) => (({} as unknown) as any)) and it will work but typescript will think that map is still a number[] | number[][] | mathjs.Matrix

dmitryrn avatar Apr 05 '20 04:04 dmitryrn

So it is a problem with type definitions

dmitryrn avatar Apr 05 '20 04:04 dmitryrn

Would be great to improve the TypeScript definitions so you know that you actually have much more power :)

Is there anyone who can help out on improving the TS definitions?

josdejong avatar Apr 05 '20 08:04 josdejong

I've raised a new issue and hours later find this one (didn't searched the correct key words).

I suggest to modify the @types/mathjs/index.d.ts on line 24

type MathArray = Number[] | Number[][];

to

type MathArray = MathType[] | MathType[][];

I've also noticed another potential typo on line 2974:

valueOff(): MathArray | Matrix;

should be

valueOf(): MathArray | Matrix;

ghost avatar Apr 24 '20 21:04 ghost

Related: #1828. Would be nice to fix this in one go as well.

The isConditionalNode attribute of MathNode is wrongly spelled isConditionalnode in @types/mathjs

Anyone able to create a PR with fixes for the TypeScript definitions?

josdejong avatar Apr 29 '20 08:04 josdejong

I made this change https://github.com/DefinitelyTyped/DefinitelyTyped/pull/45289 , however it's creating a new issue where there is a circular reference between the MathType and MathArray types -- which causes the linter to throw. Any suggestions?

Type alias 'MathType' circularly references itself.

jasonshaev avatar Jun 04 '20 18:06 jasonshaev

Thanks Jason for picking this up! Maybe it's possible to define a MathScalar type, which is all types like number and Unit, except matrix types?

josdejong avatar Jun 06 '20 11:06 josdejong