Jak zdefiniować własną definicję maszynopisu tekstu z tablicy, w którym wszystkie elementy są pokoje, z wyjątkiem pierwszego

0

Pytanie

Tablice, o których mowa, są segmentami drodze SVG, np. ['L', 0, 0] Ja w zasadzie używam dla określenia tych tablic:

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

Teraz w pathCommand.toLowerCase() wyzwanie wydaje się ten błąd:

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

I segmentValues rzuca to:

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

Tak więc, jak określić definiowanie niestandardowego typu @type {WHAT} segment) że spełnia tę konkretną potrzebę?

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

Najlepsza odpowiedź

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

Aby uzyskać więcej informacji na temat elementów rest w typach krotek: https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types

Oto przykłady z dokumentów:

Кортежи również mogą mieć elementy rest, które muszą być typu tablicy/tkę .

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

Szybkie pytanie: czy to może być podjęta dla obiektów tkę? Coś takiego {type: string, ...{string, number}[]}Sprawdzałem wszystko, ale to nie zadziałało.
thednp

Przepraszam, nie bardzo rozumiem to. Być może można by opublikować nowe pytanie o błędzie przepełnienia stosu więcej informacji?
Anastasia

To samo, ale do obiektów, a nie [string, ...number[]] czy możliwe jest mieć coś takiego {myString: string, ...{string, number}[]].
thednp

W innych językach

Ta strona jest w innych językach

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