gpt4 book ai didi

arrays - 类型 'Array[]' 不可分配给类型 '[Array]'

转载 作者:搜寻专家 更新时间:2023-10-30 21:12:35 25 4
gpt4 key购买 nike

当我试图获取 typescript 时,这个错误一直困扰着我。谁能解释这两个“数组”之间的区别是什么?这不是数组吗?如果不是,那又是什么?

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

最佳答案

您的第一个类型 ( AppointmentItemData[] ) 是 AppointmentItemData 的数组元素,而你的第二种类型是 a tuple ,它可能只包含一个单个 AppointmentItemData .引自 the docs :

Tuple types allow you to express an array where the type of a fixed number of elements is known, but need not be the same. For example, you may want to represent a value as a pair of a string and a number:

// 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 ”,而如果它不是编码 [number, string, Promise<Lizard>] 的唯一方法将是 {}[] (大致上,一个 Object[] )。

关于arrays - 类型 'Array[]' 不可分配给类型 '[Array]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40697919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com