【问题标题】:How to define a custom TypeScript definition of an array where all elements are number except first如何定义数组的自定义 TypeScript 定义,其中所有元素都是数字,除了第一个
【发布时间】:2022-01-01 04:56:33
【问题描述】:

有问题的数组是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)来满足这个特定的需求呢?

【问题讨论】:

  • 可能喜欢this?但是someVarotherVar 是什么?并且minimal reproducible example 不应该有拼写错误或其他不相关的问题(例如typedef 而不是@typedef)。请修复代码以展示您的确切问题,因为您似乎对doSomethingToSegment实现 中的内容而不是callers 有问题。跨度>
  • 你的例子看起来不错,谢谢。为什么您没有发布答案以便我将其标记为绿色?
  • 我很高兴,但我不知道someVar.toFixed(3) 会做什么,我担心我会发布答案,你会有后续关于这一点的问题。如果它不重要,那么也许将其从问题中删除?如果它很重要,你能用一个代码示例来充实它吗?
  • 编辑了问题,someVar.toFixed() 真的不重要,它是关于字符串类型变量的任何数字运算,或者当someVar 用作函数中的参数时需要一个数字..
  • 我现在正在编辑,很难做一个最简单的例子,所以任何人都不想提供答案,这对任何人来说都不是负担。谢谢。

标签: javascript typescript ecmascript-6


【解决方案1】:
type A = [string, ...number[]];

有关元组类型中剩余元素的更多信息: https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types

以下是文档中的示例:

元组也可以有剩余元素,它们必须是数组/元组 输入。

type StringNumberBooleans = [string, number, ...boolean[]];
type StringBooleansNumber = [string, ...boolean[], number];
type BooleansStringNumber = [...boolean[], string, number];

【讨论】:

  • 快速提问:这可以被 Tuple 对象采用吗?像{type: string, ...{string, number}[]} 这样的东西,我在测试一切都没有让它工作。
  • 对不起,我不太明白。也许您可以发布一个更详细的新 Stack Overflow 问题?
  • 完全相同的东西,但对于对象,而不是[string, ...number[]],是否有可能拥有类似{myString: string, ...{string, number}[]]的东西。
猜你喜欢
  • 2017-11-08
  • 1970-01-01
  • 2017-04-26
  • 1970-01-01
  • 1970-01-01
  • 2019-12-11
  • 2015-07-10
  • 2016-07-14
  • 2019-01-31
相关资源
最近更新 更多