Como definir um costume Transcrito definição de uma matriz onde todos os elementos são excepto o primeiro número

0

Pergunta

As matrizes em questão são SVG segmentos de caminho, por exemplo ['L', 0, 0] Eu basicamente estou usando isso para definir estas matrizes:

// 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));
}

Agora o pathCommand.toLowerCase() chamada de lança este erro:

Property 'toLowerCase' does not exist on type 'string | number'.
  Property 'toLowerCase' does not exist on type 'number'.

E o segmentValues joga este:

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'.

Então, como definir uma definição de tipo personalizado @type {WHAT} segment) que satisfaz essa necessidade específica?

ecmascript-6 javascript typescript
2021-11-22 15:33:56
1

Melhor resposta

2
type A = [string, ...number[]];

Mais informações sobre o resto elementos na tupla tipos: https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types

Aqui estão alguns exemplos de documentos:

Tuplas podem também ter o resto elementos, o que tem que ser uma matriz/tuple escreva.

type StringNumberBooleans = [string, number, ...boolean[]];
type StringBooleansNumber = [string, ...boolean[], number];
type BooleansStringNumber = [...boolean[], string, number];
2021-11-22 15:49:59

Pergunta rápida: isso pode ser adotada para a Tupla de objetos? Algo como {type: string, ...{string, number}[]}, Eu estava testando tudo o que não fazê-lo funcionar.
thednp

Desculpe, eu não entendo muito bem isso. Talvez você poderia postar um novo Estouro de Pilha questão com um pouco mais de detalhe?
Anastasia

Exatamente a mesma coisa, mas para objetos, em vez de [string, ...number[]] é possível ter algo como {myString: string, ...{string, number}[]].
thednp

Em outros idiomas

Esta página está em outros idiomas

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................