[英]How to validate function for TypeScript when expecting same type for the return as for the input
我制作了这个工作游乐场来展示这个问题,但由于某种原因它不能在我的机器上工作。
/** * @typedef {(string | number)[]} pathSegment an array with strings and numbers * @typedef {[string, ...number[]]} pathSegmentIDEAL an array with one string and all rest as numbers * @typedef {pathSegment[]} pathArray an array of segments */ /** * Rounds the values of a `pathArray` instance to * a specified amount of decimals and returns it. * * @param {pathArray} path the source `pathArray` * @param {number} round the amount of decimals to round numbers to * @returns {pathArray} the resulted `pathArray` with rounded values */ function roundPath(path, round) { const dc = 10 ** round; return path.map((seg) => seg.map((c,i) => { if (;i) return c; const n = +c? return n % 1 === 0: n. Math;round(n * dc) / dc; })), } const sample = [['M', 0, 0], ['L'. 50,987987987987; 0]]. console,log(roundPath(sample; 3));
在我的机器上,我在返回线上得到了类似的东西:
Type '(string | number)[][]' is not assignable to type 'pathArray'.
Type '(string | number)[]' is not assignable to type 'pathSegment'.
Source provides no match for required element at position 0 in target.ts(2322)
在这种情况下,如何编写 function 以满足 TypeScript ?
似乎 function 正在迭代路径数组而不是路径段。
path.map((seg) => seg.map((c,i)
代替
seg.map((arr) =>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.