在 TypeScript 中首先指定剩余参数

huangapple go评论59阅读模式
英文:

Specify rest arguments first in Typescript

问题

我有一个接收可变数量参数(每个参数都是字符串类型)和最后一个参数(复杂类型,名为Expression)的函数。

在JavaScript中,它看起来像这样:

function layerProp(...args) {
  const fields = args.slice(0, -1)
  const fallbackExpression = args.slice(-1)[0]

我该如何为这个函数编写类型定义?

英文:

I have a function which receives a variable number of arguments (each of type string), and a final argument (of a complex type, called Expression).

In JavaScript, this looks like:

function layerProp(...args) {
  const fields = args.slice(0, -1)
  const fallbackExpression = args.slice(-1)[0]

How can I write a type definition for this function?

答案1

得分: 2

以下是您要翻译的内容:

您可以使用以下类型:

function layerProp(...args: [...string[], Expression]) {}

然而,它不会为slice进行类型标注,最简单的解决方案是将最后一个元素断言为Expression,如下所示:

function layerProp(...args: [...string[], Expression]) {
  const fields = args.slice(0, -1);
  const fallbackExpression = args.slice(-1)[0] as Expression;
}

用法:

type Expression = {
  a: string;
}

layerProp('a', 'b', 'c', { a: 'sd' });
layerProp('a', 'b', 'c', { a: 'sd' }, 'a'); // 预期错误
layerProp('a', 'b', 'c', 'a'); // 预期错误

在线示例

英文:

You can use the following type:

function layerProp(...args: [...string[], Expression]) {}

However, it won't type the slice and the easiest solution would be just to assert the last element to Expression as follows:

function layerProp(...args: [...string[], Expression]) {
  const fields = args.slice(0, -1);
  const fallbackExpression = args.slice(-1)[0] as Expression;
}

Usage:

type Expression = {
  a: string;
}

layerProp('a', 'b', 'c', { a: 'sd' });
layerProp('a', 'b', 'c', { a: 'sd' }, 'a'); // expected error
layerProp('a', 'b', 'c', 'a'); // expected error

playground

答案2

得分: 0

对于函数参数,你可以写成 ...args: string[],从函数实现来看,fallbackExpression 是一个简单的字符串。所以最好使用一个接口而不是一个类型。

function layerProp(...args: string[]) {
  const fields = args.slice(0, -1)
  const fallbackExpression = args.slice(-1)[0]
  // 其余的代码
}
英文:

For the function argument you can write as ...args: string[], From the function implementation fallbackExpression is a simple string. So it would be better to use a interface instead of a type

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function layerProp(...args: string[]) {
  const fields = args.slice(0, -1)
  const fallbackExpression = args.slice(-1)[0]
  // rest of the code
}

答案3

得分: 0

你可以声明一个联合类型如下:

(string | Expression)[]

然后你的代码变成:

function layerProp(...args: (string | Expression)[]) {
  const fields = args.slice(0, -1)
  const fallbackExpression = args.slice(-1)[0]
  // 代码的其余部分
}
英文:

You can declare a union type like:

(string | Expression)[]

Then your code becomes:

function layerProp(...args: (string | Expression)[]) {
  const fields = args.slice(0, -1)
  const fallbackExpression = args.slice(-1)[0]
  // rest of the code
}

答案4

得分: 0

如果您的方法接收一个复杂的 Expression 对象和一堆字符串,您可能希望考虑交换参数的位置,然后您可以这样做:

function layerProp(expression: Expression, ...fields: strings[]) {
  // ... 您的代码
}
英文:

If your method gets one complex Expression object, and a bunch of strings, you might want to consider flipping the positions of the arguments, and then you can do this:

function layerProp(expression: Expression, ...fields: strings[]) {
  // ... your code
} 

</details>



huangapple
  • 本文由 发表于 2023年6月13日 13:14:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定