英文:
Typescript error using spread operator in function
问题
我正在尝试在sendMessage
函数中使用扩展运算符进行WebSocket通信。我收到了一个错误,说扩展参数必须是元组类型或传递给rest参数。sendMessage
函数使用...values
作为rest参数。我错过了什么?
我的问题是测试可能具有任意长度(但始终包含一个元素)。
const sendMessage = (type: string, ...values: (string | number)[]) =>
websocket.connected_socket.send(`${type}=${values.join("~")}`);
let test = ["1", "2", "3", "4", "5"]
sendMessage(...test) // 错误:扩展参数必须要么是元组类型,要么传递给rest参数.ts(2556)
sendMessage(...["1", "2", "3", "4", "5"]) // 这是可以的
编辑:这个方法有效。我猜与函数的参数有关。
sendMessage(test[0], ...test.slice(1))
英文:
I am trying to use the spread operator in the sendMessage
function for websockets. I get the error that the spread argument must either have a tuple type or be passed to a rest parameter. sendMessage
uses ...values
as a rest parameter. What am I missing?
My problem is that test could be of arbitrary length (but will always contain one element).
const sendMessage = (type: string, ...values: (string | number)[]) =>
websocket.connected_socket.send(`${type}=${values.join("~")}`);
let test = ["1", "2", "3", "4", "5"]
sendMessage(...test) // error: A spread argument must either have a tuple type or be passed to a rest parameter.ts(2556)
sendMessage(...["1", "2", "3", "4", "5"]) // this is fine
EDIT: This works. I guess it had something to do with the parameters of the function
sendMessage(test[0], ...test.slice(1))
答案1
得分: 2
尝试使用 as const
:
const test = ["1", "2", "3", "4", "5"] as const
英文:
Try as const
:
const test = ["1", "2", "3", "4", "5"] as const
答案2
得分: 1
以下是翻译好的部分:
默认的 test
类型将是一个 string[]
。错误提到了扩展参数可以是一个元组,但这里我们有一个数组。
元组 就像一个数组,但长度是固定的,而且每个索引处的元素类型也是固定的。
const sendMessage = (type: string, ...values: (string | number)[]) => {
};
如果我们展开一个空数组,将得不到任何元素。函数将无法按预期工作。因此,扩展数组类型是不允许的,这正是发生的情况。
为了传递正确的参数,最好更加限制性。元组就是这样做的方法。
- 像这样定义
test
:
const test: [string, string, string, string, string] = ["1", "2", "3", "4", "5"]
- 如另一个答案中所提到的,使用
const
断言。它会给出 TypeScript 能够推断的最精确类型。数组字面量变成只读元组。
const test = ["1", "2", "3", "4", "5"] as const;
英文:
There are a couple of ways to solve this. But let us see why the error arises.
The default type of test
will be an string[]
. The error mentions that spread argument can be a tuple, but here we have an array.
A tuple is just like an array but the length is fixed and also the type of element at each index is fixed.
const sendMessage= (type: string, ...values: (string | number)[]) => {
};
If we spread an empty array, we would get no elements. The function would not work as expected. Hence spearing an array type is something that should be disallowed, which is exactly what is happening.
To pass the correct arguments, it is better to be more restrictive. And tuple is the way to do it.
- Define
test
like this:
const test : [string,string,string,string,string]= ["1", "2", "3", "4", "5"]
- As mentioned in another answer, use
const
assertion. It will give the most exact type typescript can infer. Array literals become readonly tuples.
const test = ["1", "2", "3", "4", "5"] as const;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论