วิธีที่จะกำหนดกำหนดเอง TypeScript นิยามของอาเรย์อยู่ไหนทั้งส่วนประกอบเป็นจำนวนยกเว้นคนแรก

0

คำถาม

ที่ arrays อยู่ในคำถามคือ SVG เส้นทางเซกเมนต์สำหรับตัวอย่าง ['L', 0, 0] ฉันก็ใช้นี่เพื่อกำหนดพวกนี้ arrays:

// 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[]];

ข้อมูลเพิ่มเติมเรื่องที่เหลือส่วนประกอบใน tuple ประเภท: https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types

นี่คือตัวอย่างจาก docs:

Tuples ยังสามารถมีส่วนประกอบที่เหลือซึ่งจำเป็นต้องเป็นอาเรย์/tuple ประเภทนี้ได้นะ

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

ขอถาม:นี่มันสามารถได้รับเลี้ยงสำหรับ Tuple™à§à±à•à-à? บางอย่างอย่าง {type: string, ...{string, number}[]}ฉันกำลังทดสอบทุกอย่างไม่ได้เอามันไปทำงานแล้ว
thednp

ขอโทษฉันไม่ค่อยเข้าใจมัน บางทีคุณอาจจะโพสใหม่ดิเลยล้นมาถึงคำถามกับอีกหน่อยได้มัยกลับมารายละเอียด?
Anastasia

เหมือนกันเรื่องแต่สำหรับวัตถุแทนที่จะเป็น [string, ...number[]] มันเป็นไปได้ที่จะมีบางอย่างอย่าง {myString: string, ...{string, number}[]].
thednp

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

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

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่