error TS2339: Property 'subset' does not exist on type 'MathType'. Property 'subset' does not exist on type 'number'.
import * as math from "mathjs"
const d = math.matrix([
[1, 2],
[3, 4],
])
var res = math.dotPow(d, 2)
console.log(res.subset(math.index(1, 1)))
run js code, get the expected result: print 16
When I put the same js code in ts file
t1.ts file
import * as math from "mathjs"
const d = math.matrix([
[1, 2],
[3, 4],
])
var res = math.dotPow(d, 2)
console.log(res.subset(math.index(1, 1)))
and run ts-node t1.ts
,but get the following output errors:
/Users/felix/.nvm/versions/node/v18.4.0/lib/node_modules/ts-node/src/index.ts:843
return new TSError(diagnosticText, diagnosticCodes, diagnostics);
^
TSError: ⨯ Unable to compile TypeScript:
t1.ts:7:17 - error TS2339: Property 'subset' does not exist on type 'MathType'.
Property 'subset' does not exist on type 'number'.
7 console.log(res.subset(math.index(1, 1)))
~~~~~~
at createTSError (/Users/felix/.nvm/versions/node/v18.4.0/lib/node_modules/ts-node/src/index.ts:843:12)
at reportTSError (/Users/felix/.nvm/versions/node/v18.4.0/lib/node_modules/ts-node/src/index.ts:847:19)
at getOutput (/Users/felix/.nvm/versions/node/v18.4.0/lib/node_modules/ts-node/src/index.ts:1057:36)
at Object.compile (/Users/felix/.nvm/versions/node/v18.4.0/lib/node_modules/ts-node/src/index.ts:1411:41)
at transformSource (/Users/felix/.nvm/versions/node/v18.4.0/lib/node_modules/ts-node/src/esm.ts:400:37)
at /Users/felix/.nvm/versions/node/v18.4.0/lib/node_modules/ts-node/src/esm.ts:278:53
at async addShortCircuitFlag (/Users/felix/.nvm/versions/node/v18.4.0/lib/node_modules/ts-node/src/esm.ts:409:15)
at async ESMLoader.load (node:internal/modules/esm/loader:431:20)
at async ESMLoader.moduleProvider (node:internal/modules/esm/loader:350:11)
at async link (node:internal/modules/esm/module_job:70:21) {
diagnosticCodes: [ 2339 ]
}
The reason is that the method .subset() is only available on a Matrix, and the output of math.dotPow is currently not specific enough to know that it res is indeed a Matrix.
Here is the current type definition of dotPow:
https://github.com/josdejong/mathjs/blob/ead6228939fdf8a2d1f301f96e346a942e60adc4/types/index.d.ts#L1085
This definition should be changed similar to most functions (like exp that is right below it):
https://github.com/josdejong/mathjs/blob/ead6228939fdf8a2d1f301f96e346a942e60adc4/types/index.d.ts#L1093-L1097
Anyone able to improve the type definitions of dotPow and also dotMultiply and dotDivide?
Fixed now in v11.5.1 via #2890.