을 정의하는 방법을 사용자 정의 타이프 라이터의 모든 요소가 번호를 제외하고 먼저

0

질문

배열에 질문 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) 이것을 만족하는 특정 필요로 하는가?

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

최고의 응답

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

더 많은 정보에 대한 나머지 요소에서 튜플 유형: https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types

여기에서 예 docs:

튜플 수 있다 또한 나머지 요소가 있을 배열/tuple 유형입니다.

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

빠른 질문이 얻을 채택을 위한 튜플까? 뭔가 {type: string, ...{string, number}[]}었 모든 것을 테스트하지 않았다.
thednp

Sorry,내가 확실히 이해하지 않습니다. 어쩌면 당신은 게시할 수 있는 새로운 Stack Overflow 질문을 조금 더 자세히?
Anastasia

똑같은 일을 하지만 개체에 대한 대 [string, ...number[]] 그것은 무언가가 있음 {myString: string, ...{string, number}[]].
thednp

다른 언어로

이 페이지는 다른 언어로되어 있습니다

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