我正在使用由useFirestore()提供的这个VueUse.org包装器
我正在尝试键入它与User一起返回的User引用,如下所示:
import { User } from 'src/types';
const userId = useUserStore().user.id;
const userDocRef = doc(db, 'users', userId);
const user = useFirestore<User>(userDocRef); // <-- Error shows here这就是我的User界面的样子:
export interface User {
id: string;
userType?: UserType;
createdAt?: Timestamp;
email?: string;
displayName?: string;
firstName?: string;
lastName?: string;
initials?: string;
passwordUpdatedAt?: Timestamp;
photoUrl?: string;
phoneE164Format?: string;
phoneNationalFormat?: string;
stripeId?: string;
stripeLink?: string;
googleTokens?: {
access_token: string;
expiry_date: number;
id_token: string;
refresh_token: string;
token_type: string;
};
}这个很管用。user变量现在具有带有User属性的intellisense。
但是它也会在userDocRef上产生这个错误。
没有过载和这个电话匹配。重载4中的1,'(maybeDocRef: MaybeRef,initialValue?:UseFirestoreOptions,options?:UseFirestoreOptions error?):Ref<..>‘,给出了以下错误。重载4中的2,'(maybeDocRef: MaybeRef,initialValue?:User[]财政未定义,options?:UseFirestoreOptions财政未定义):Ref<.>‘,给出了以下error.ts(2769)
在useFirestore()文档的底部中有一个名为“类型声明”的部分,它可能会有所帮助。但我很难完全理解。
我认为通过将User作为泛型传递是正确的。所以我不知道为什么会有这个错误。
为什么我看到这个错误,以及如何修复它?
发布于 2022-08-19 04:07:10
我想通了。
我的接口有一个必需的id属性,它导致了冲突。
以下是完整的最终代码:
const userId = useUserStore().user.id;
const userDocRef = doc(db, 'users', userId);
const user = useFirestore<User>(userDocRef);export interface User {
id?: string; // <-- Updated here
userType?: UserType;
createdAt?: Timestamp;
email?: string;
displayName?: string;
firstName?: string;
lastName?: string;
initials?: string;
passwordUpdatedAt?: Timestamp;
photoUrl?: string;
phoneE164Format?: string;
phoneNationalFormat?: string;
stripeId?: string;
stripeLink?: string;
googleTokens?: {
access_token: string;
expiry_date: number;
id_token: string;
refresh_token: string;
token_type: string;
};
}这回答了这个问题并解决了我眼前的问题,但我不知道为什么我需要所有的接口属性都是可选的。这似乎很有限制性。
如果有人知道为什么,请在下面评论。
https://stackoverflow.com/questions/73411662
复制相似问题