Come definire un custom Dattiloscritto definizione di un array in cui tutti gli elementi sono i numeri ad eccezione di prima

0

Domanda

Le matrici in questione sono SVG segmenti di percorso, per esempio ['L', 0, 0] Praticamente sto usando questo per definire queste matrici:

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

Ora il pathCommand.toLowerCase() chiamata genera questo errore:

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

E il segmentValues lancia questo:

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

Così, come definire un tipo personalizzato definizione @type {WHAT} segment) che soddisfa questa esigenza specifica?

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

Migliore risposta

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

Più informazioni su resto elementi di una tupla tipi: https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types

Qui ci sono esempi di documenti:

Le tuple possono anche riposare elementi, che devono essere un array/tupla tipo.

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

Domanda veloce: è possibile ottenere questo ha adottato per Tupla oggetti? Qualcosa di simile {type: string, ...{string, number}[]}, Stavo testando il tutto non funziona.
thednp

Mi dispiace, non riesco a capire è. Forse si potrebbe postare un nuovo Stack Overflow domanda con un po ' più in dettaglio?
Anastasia

Esattamente la stessa cosa, ma per gli oggetti, invece di [string, ...number[]] e ' possibile avere qualcosa di simile {myString: string, ...{string, number}[]].
thednp

In altre lingue

Questa pagina è in altre lingue

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