这些阵列在的问题是SVG路段,例如 ['L', 0, 0]
我基本上使用这一定义这些阵列:
// doSomethingToSegment.js
/** @type {Object.<string, number>} */
const paramsCount = {
a: 7, c: 6, h: 1, l: 2, m: 2, r: 4, q: 4, s: 4, t: 2, v: 1, z: 0,
};
/**
* This definition is WRONG, FIX ME!!
*
* @typedef {(string|number)[]} segment
*/
/**
* Check segment validity.
*
* @param {segment} seg input segment
* @return {boolean} segment is/not valid
*/
function checkSegment(seg) {
const [pathCommand] = seg;
const LK = pathCommand.toLowerCase();
const UK = pathCommand.toUpperCase();
const segmentValues = seg.slice(1);
const expectedAmount = paramsCount[LK];
return checkPathCommand(UK) && checkPathValues(segmentValues, expectedAmount);
}
/**
* @param {string} ch input character
* @returns {boolean} true when `ch` is path command
*/
function checkPathCommand(ch) {
return ('ACHLMRQSTVZ').includes(ch);
}
/**
* @param {Number[]} values input values
* @param {Number} expected amount
* @return {boolean} segment has/not the right amount of valid numbers
*/
function checkPathValues(values, expected) {
return values.length === expected && values.every(x => !Number.isNaN(x));
}
现在 pathCommand.toLowerCase()
呼叫抛出这个错误:
Property 'toLowerCase' does not exist on type 'string | number'.
Property 'toLowerCase' does not exist on type 'number'.
和 segmentValues
引发这一:
Argument of type '(string | number)[]' is not assignable to parameter of type 'number[]'.
Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.
因此,如何定义的一个定义类型的定义 @type {WHAT} segment)
满足这种具体的需要?
{type: string, ...{string, number}[]}
我是测试的一切没有得到它的工作。