类型“ Array []”不可分配给类型“ [Array]” [英] Type 'Array[]' is not assignable to type '[Array]'

查看:67
本文介绍了类型“ Array []”不可分配给类型“ [Array]”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试获取打字稿时,此错误一直困扰着我。谁能解释两个数组之间的区别?这不是数组吗?如果不是,那是什么?

This error keeps bugging me while I'm trying to get a grab on typescript. Can anyone explain what is the difference between the two 'array'? Is any of this not an array? If not, what is it then?

[ts] Type 'AppointmentItemData[]' is not assignable to type '[AppointmentItemData]'.
       Property '0' is missing in type 'AppointmentItemData[]'.


推荐答案

您的第一种类型( AppointmentItemData [] )是 AppointmentItemData 元素的数组,而第二种类型是一个元组,该元组只能包含单个 AppointmentItemData 。引用文档

Your first type (AppointmentItemData[]) is an array of AppointmentItemData elements, while your second type is a tuple, that may only ever contain a single AppointmentItemData. Quoting from the docs:


元组类型允许您表达一个数组,在该数组中已知固定数量的元素的类型,但不必相同。例如,您可能希望将值表示为一对字符串数字

// Declare a tuple type
let x: [string, number];
// Initialize it
x = ["hello", 10]; // OK
// Initialize it incorrectly
x = [10, "hello"]; // Error


Typescript可让您编码该变量应只有每个元素中都有3个元素的数组,其中第一个元素是数字,第二个元素是字符串,第三个元素是Promise,最终将解析为形状如 Lizard ,而如果不是编码 [数字,字符串,Promise< Lizard>] 的唯一方法将是 {} [ ] (大致是 Object [] )。

Typescript allows you to encode that "this variable should only every have a 3 element array in it, where the first element is a number, the second is a string, and the third is a Promise that will eventually resolve to something shaped like a Lizard", whereas if it didn't the only way to encode [number, string, Promise<Lizard>] would be {}[] (that is, roughly, an Object[]).

这篇关于类型“ Array []”不可分配给类型“ [Array]”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆