admin管理员组文章数量:1220423
I am trying to destructure an array of length 2, but I get a typescript error:
[ts] Tuple type '[string]' with length '1' cannot be assigned
to tuple with length '2'.
let output = {status: false};
if(execute.permission) {
let message: [string] = execute.params;
if(message.length >= 2) {
// Destructuring follows
[output['position'], output['message']] = message;
}
}
How do I tell typescript, that the array could possiblly be of length 2?
I am trying to destructure an array of length 2, but I get a typescript error:
[ts] Tuple type '[string]' with length '1' cannot be assigned
to tuple with length '2'.
let output = {status: false};
if(execute.permission) {
let message: [string] = execute.params;
if(message.length >= 2) {
// Destructuring follows
[output['position'], output['message']] = message;
}
}
How do I tell typescript, that the array could possiblly be of length 2?
Share Improve this question asked Mar 31, 2017 at 1:13 Suhail GuptaSuhail Gupta 23.3k66 gold badges211 silver badges344 bronze badges 3 |3 Answers
Reset to default 11You've not declared message
as an array; you've declared it as a tuple ([string]
) and tuples have a fixed number of elements. (See the Tuple section in the Basic Types documentation.)
You could declare it as a tuple that has two string elements ([string, string]
), but given that you are testing message.length >= 2
it seems likely you intended to declare it as a string array (string[]
):
let output = {status: false};
if(execute.permission) {
let message: string[] = execute.params;
if(message.length >= 2) {
// Destructuring follows
[output['position'], output['message']] = message;
}
}
Use string[]
(an array of any length) instead of [string]
(a "tuple" limited to length 1) as your type.
Tuples have a specific length and make it easier to represent multiple types assigned to specific index positions, like [string, number]
. Homogenous (single-type) tuples are still useful in some scenarios (such as representing pairs in a map), but are not as common.
Arrays, on the other hand, are lists of variable length but are designed to hold only references of a single type (even if that type is a union type or any
). If one index of any array can hold a certain value, every index can hold that same kind of value.
TypeScript Code (Playground Link)
let execute = { permission: true, params: ['a', 'b']}
let output = { status: false };
if(execute.permission) {
let message: string[] = execute.params;
if(message.length >= 2) {
// Destructuring follows
[output['position'], output['message']] = message;
}
}
console.log(output)
Try to change type of message
variable. Array of strings suits better than array with single string
let message: string[] = execute.params;
本文标签: javascriptError while trying to destructure an array of possible length 2Stack Overflow
版权声明:本文标题:javascript - Error while trying to destructure an array of possible length 2 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739244429a2154614.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
let message: [string, string] = execute.params;
– Diullei Commented Mar 31, 2017 at 1:23[string]
instead ofstring[]
. The former creates a tuple, and the latter creates an array. See typescriptlang.org/docs/handbook/2/everyday-types.html – Janac Meena Commented Jun 27, 2021 at 20:21