gpt4 book ai didi

React Typescript issue(反应打字稿问题)

转载 作者:bug小助手 更新时间:2023-10-28 11:10:19 28 4
gpt4 key购买 nike



`issue: The expected type comes from property 'listing' which is declared here on type
'IntrinsicAttributes & ListingClientProps'

`Issue:预期类型来自此处在类型‘IntrinsicAttributes&ListingClientProps’上声明的属性‘Listing’


I've tried many things but still not working

我试了很多方法,但还是不管用


SafeListing and SafeUser are Types..
and I don't get why I'm getting error
can anyone help with that?

SafeListing和SafeUser是类型..我不明白为什么我会出错,有谁能帮忙吗?


ListingClient.tsx

ListingClient.tsx



interface ListingClientProps {
reservations?: SafeReservation[];
listing: SafeListing & {
user: SafeUser;
};
currentUser?: SafeUser | null;
}

const ListingClient: React.FC<ListingClientProps> = ({
listing,
currentUser,
reservations = [],
}) => {
// rest of the code
}


page.tsx

Page.tsx


import getCurrentUser from "@/app/actions/getCurrentUser";
import getListingById from "@/app/actions/getListingById";
import ClientOnly from "@/app/components/ClientOnly";
import EmptyState from "@/app/components/EmptyState";
import ListingClient from "./ListingClient";
import getReservations from "@/app/actions/getReservation";

interface IParams {
listingId?: string;
}

const ListingPage = async ({ params }: { params: IParams }) => {
const listing = await getListingById(params);
const reservations = await getReservations(params);
const currentUser = await getCurrentUser();

if (!listing) {
return (
<ClientOnly>
<EmptyState />
</ClientOnly>
);
}

return (
<ClientOnly>
<ListingClient listing={listing} currentUser={currentUser} reservations={reservations} />
</ClientOnly>
);
};

export default ListingPage;

types.ts

Types.ts


import { Listing, Reservation, User } from "@prisma/client";

export type SafeListing = Omit<Listing, "createdAt"> & {
createdAt: string;
};

export type SafeReservation = Omit<
Reservation,
"createdAt" | "startDate" | "endDate" | "listing"
> & {
createdAt: string;
startDate: string;
endDate: string;
listing: SafeListing;
};

export type SafeUser = Omit<User, "createdAt" | "updatedAt" | "emailVerified"> & {
createdAt: string;
updatedAt: string;
emailVerified: string | null;
};


`

`


ERORR MESSAGE

ERORR消息


[{
"resource": "/Users/macbook/Desktop/projects_new/airbnb-clone/app/listings/[listingId]/page.tsx",
"owner": "typescript",
"code": "2322",
"severity": 8,
"message": "Type '{ createdAt: string; user: { createdAt: string; updatedAt: string; emailVerified: (() => string) | null; id: string; name: string | null; email: string | null; image: string | null; hashedPassword: string | null; favoriteIds: string[]; id_token: string | null; }; ... 10 more ...; price: number; }' is not assignable to type 'Omit<{ id: string; title: string; description: string; imageSrc: string; createdAt: Date; category: string; roomCount: number; bathroomCount: number; guestCount: number; locationValue: string; userId: string; price: number; }, "createdAt"> & { ...; } & { ...; }'.\n Type '{ createdAt: string; user: { createdAt: string; updatedAt: string; emailVerified: (() => string) | null; id: string; name: string | null; email: string | null; image: string | null; hashedPassword: string | null; favoriteIds: string[]; id_token: string | null; }; ... 10 more ...; price: number; }' is not assignable to type '{ user: SafeUser; }'.\n Types of property 'user' are incompatible.\n Type '{ createdAt: string; updatedAt: string; emailVerified: (() => string) | null; id: string; name: string | null; email: string | null; image: string | null; hashedPassword: string | null; favoriteIds: string[]; id_token: string | null; }' is not assignable to type 'SafeUser'.\n Type '{ createdAt: string; updatedAt: string; emailVerified: (() => string) | null; id: string; name: string | null; email: string | null; image: string | null; hashedPassword: string | null; favoriteIds: string[]; id_token: string | null; }' is not assignable to type '{ createdAt: string; updatedAt: string; emailVerified: string | null; }'.\n Types of property 'emailVerified' are incompatible.\n Type '(() => string) | null' is not assignable to type 'string | null'.\n Type '() => string' is not assignable to type 'string'.",
"source": "ts",
"startLineNumber": 27,
"startColumn": 25,
"endLineNumber": 27,
"endColumn": 32,
"relatedInformation": [
{
"startLineNumber": 49,
"startColumn": 4,
"endLineNumber": 49,
"endColumn": 11,
"message": "The expected type comes from property 'listing' which is declared here on type 'IntrinsicAttributes & ListingClientProps'",
"resource": "/Users/macbook/Desktop/projects_new/airbnb-clone/app/listings/[listingId]/ListingClient.tsx"
}
]
}]

[{“资源”:“/Users/macbook/Desktop/projects_new/airbnb-clone/app/listings/[listingId]/page.tsx”,“所有者”:“类型脚本”,“代码”:“2322”,“严重性”:8,“消息”:“类型‘{createdAt:字符串;用户:{createdAt:字符串;更新日期:字符串;电子邮件验证:(()=>字符串)|空;id:字符串;名称:字符串|空;电子邮件:字符串|空;图像:字符串|空;hashedPassword:字符串|空;FavoriteIds:STRING[];id_TOKEN:STRING|NULL;};...10 MORE...;PRICE:NUMBER;}‘不可分配给类型’OMIT<{id:STRING;TITLE:STRING;DESCRIPTION:STRIT;ImageSrc:STRING;createdAt:DATE;CATEGORY:STRING;ROOMCOUNT:NUMBER;bathroomCount:NUMBER;LOCATIOVALUE:STRING;UserID:STRING;PRICE:NUMBER;},“createdAt”>&{...;}&{...;}‘。\n类型’{createdAt:STRING;用户:{createdAt:字符串;updatedAt:字符串;电子邮件验证:(()=>字符串)|空;id:字符串;名称:字符串|空;电子邮件:字符串|空;图像:字符串|空;hashedPassword:字符串|空;FavoriteIds:字符串[];id_Token:字符串|空;};...10更多...;价格:数字;}‘不可分配给类型’{User:SafeUser;}‘。\n属性’User‘的类型不兼容。\n类型’{createdAt:字符串;updatedAt:字符串;电子邮件验证:(()=>字符串)|NULL;id:字符串;名称:字符串|空;电子邮件:字符串|空;图像:字符串|空;hashedPassword:字符串|空;收藏夹ID:字符串[];id_Token:字符串|空;}‘不能分配给类型’SafeUser‘。\n类型’{createdAt:字符串;更新日期:字符串;电子邮件验证:(()=>字符串)|空;id:字符串;名称:字符串|空;电子邮件:字符串|空;图像:字符串|空;hashedPassword:字符串|空;收藏夹:字符串[];ID_TOKEN:STRING|NULL;}‘不可分配给类型’{createdAt:STRING;updatdAt:STRING;EMAILVERIZED:STRING|NULL;}‘。\n属性’emailVerify‘的类型不兼容。\n类型’(()=>字符串)|NULL‘不可分配给类型’字符串|NULL‘。\n类型’()=>字符串‘不可分配给类型’字符串‘。“,”源“:”ts“,”startLineNumber“:27,”startColumn“:25,”endLineNumber“:27,”endColumn“:32,”relatedInformation“:[{”startLineNumber“:49,”startColumn“:4,”endLineNumber“:49,“endColumn”:11,“Message”:“预期类型来自此处在类型‘IntrinsicAttributes&ListingClientProps’上声明的属性‘Listing’”,“RESOURCE”:“/Users/macbook/Desktop/projects_new/airbnb-clone/app/listings/[listingId]/ListingClient.tsx”}]}]


更多回答

Show the full error message

显示完整的错误消息

check, I've edited the post

检查,我已经编辑了这篇帖子

The relevant code might not be posted. The root type error is Types of property 'emailVerified' are incompatible.\n Type '(() => string) | null' is not assignable to type 'string | null'. so the issue might be related to SafeListing

相关代码可能不会发布。根类型错误是属性‘emailVerify’的类型不兼容。\n类型‘(()=>字符串)|NULL’不可分配给类型‘STRING|NULL’。因此,该问题可能与安全列表有关

Your whole usage of omit and keys and taking an intersection with a type redefining them...appears kinda useless? It looks like you are trying to use a technique from nominally typed system to differentiate between validated and unsafe input, but TS is structurally typed, so if I do type Unsafe { a: string, b: number }; type Safe = Omit<Unsafe, 'a'> & { a: string } then I can still pass unsafe data to a function that is supposed to only accept validated sanitized input: the two are effectively identical types.

你对省略和键的全部使用,以及与重新定义它们的类型的交集……看起来有点没用?看起来您试图使用名义类型系统中的一种技术来区分有效输入和不安全输入,但TS是结构化类型的,所以如果我确实输入了unSafe{a:string,b:number};type Safe=omit&{a:string},那么我仍然可以将不安全的数据传递给一个应该只接受经过验证的过滤输入的函数:这两者实际上是相同的类型。

优秀答案推荐

solved problem,

解决了问题,


npx prisma db push

changed my prisma schema.

更改了我的Prisma模式。


更多回答

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