Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何在map函数中为一组非结构化对象定义接口?_Reactjs_Typescript_Destructuring - Fatal编程技术网

Reactjs 如何在map函数中为一组非结构化对象定义接口?

Reactjs 如何在map函数中为一组非结构化对象定义接口?,reactjs,typescript,destructuring,Reactjs,Typescript,Destructuring,这就是我到目前为止所做的。我尝试了许多可能的解决方案,但我一直收到下面同样的错误。任何线索或信息都会非常有用 interface RequestedOptions { name: string; } interface TicketOptions { serviceId: string; description: string; requestedAt: string; status: string; requester: RequestedOpt

这就是我到目前为止所做的。我尝试了许多可能的解决方案,但我一直收到下面同样的错误。任何线索或信息都会非常有用

interface RequestedOptions {
   name: string;
}

interface TicketOptions {
    serviceId: string;
    description: string;
    requestedAt: string;
    status: string;
    requester: RequestedOptions;
}

const tickets = transaction?.localTickets || [];

{tickets.map(
    ({ serviceId, description, createdAt, status, requester }: TicketOptions) => (
        <div className={styles.row} key={serviceId}>
        ...

似乎
transaction.localTickets
具有类型

interface TicketOptions {
    readonly serviceId: string | null;
    readonly description: string | null;
    readonly requestedAt: string | null;
    readonly status: string | null;
    readonly requester: RequestedOptions | null;
}
其中
RequestedOptions

interface RequestedOptions {
   name: string | null;
}

因此TS认为您的
TicketOptions
类型和
transaction.localTickets
不兼容。如上图所示,只需创建
TicketOptions
RequestedOptions
,就可以解决您的问题。

似乎
事务。localTickets
具有类型

interface TicketOptions {
    readonly serviceId: string | null;
    readonly description: string | null;
    readonly requestedAt: string | null;
    readonly status: string | null;
    readonly requester: RequestedOptions | null;
}
其中
RequestedOptions

interface RequestedOptions {
   name: string | null;
}

因此TS认为您的
TicketOptions
类型和
transaction.localTickets
不兼容。如上图所示,只需做出
TicketOptions
RequestedOptions
,就能解决您的问题。

非常感谢您的建议。不幸的是,它仍然导致了一个错误。在办公室里四处询问,我们有一个实用程序断言函数,可以简单地过滤空值。这就成功了。非常感谢你的建议。不幸的是,它仍然导致了一个错误。在办公室里四处询问,我们有一个实用程序断言函数,可以简单地过滤空值。这就成功了。