Replies: 8 comments 9 replies
-
PossiblyReadAsUndefined only allow you to read as undefined All type will be converted to required because source: https://firebase.google.com/docs/firestore/manage-data/data-types if you try to write few solutions:
if(input.photoUrl){
// proceed to write...
// starting from typescript version 4.6 or 4.7 or 4.8 (I cant remember),
// TS aware that input.photoUrl is not undefined in this scope
// which mean you dont need to assign extra variable
}else{
// handle undefined case
} const newProfile:UserProfile={
// ... other props
photoUrl: input.photoUrl! // typescript will think this is string
} this is not a recommended practice because it is lazy and unsafe
type UserProfile = {
tag: string;
searchTag: string;
id: string;
name: string;
photoUrl: string | null; // string or null
bio: string;
};
const newProfile:UserProfile={
// ... other props
photoUrl: input.photoUrl || null
} but
const newProfile:UserProfile={
// ... other props
photoUrl: input.photoUrl || ""
} this is the solution which I think is the best It is simple, you don't need to handle the |
Beta Was this translation helpful? Give feedback.
-
Thanks for the quick response! This all makes sense but I've been using in my project so this kinda breaks my existing code that I was trying to convert.
|
Beta Was this translation helpful? Give feedback.
-
just use the 4th solution, it will not change anything |
Beta Was this translation helpful? Give feedback.
-
the set operation requires all member to be present if you want set operation that doesnt require all member to be present, use set merge |
Beta Was this translation helpful? Give feedback.
-
Hmmm... alright. I think some of those will work although its a little annoying. Can I ask why (conceptually) set requires all members to be present? If its type is |
Beta Was this translation helpful? Give feedback.
-
Also, thank you for all the work on this. I am trying to convert as much of my code to strict typing as I can and this library is a big step. |
Beta Was this translation helpful? Give feedback.
-
if(input.photoUrl){
const newProfile:UserProfile={ // all members are present
// ... other props
photoUrl: input.photoUrl
}
batch.set(profileCol.doc(newProfile.id),newProfile)
}else{
// handle undefined case
const newProfile:UserProfile={ // not all members are present
// ... all props except photoUrl
}
batch.set(profileCol.doc(newProfile.id), newProfile, { merge : true })
} when merge is true, set will accept data with incomplete member |
Beta Was this translation helpful? Give feedback.
-
keep in mind that if you using set merge, there is no way firelord know what member is not existing so in your case you need to union photoUrl with to understand the purpose of |
Beta Was this translation helpful? Give feedback.
-
I wanted to try Firelord / FirelordJS for my Next.js project. I have a type named UserProfile:
I then turned it into a Firelord metatype:
And finally I tried to write a doc to my database. It seems that all fields that may be undefined in the original type are converted to being required. Is there a way around this? I found the docs on
PossiblyReadAsUndefined
but that seems to cover a different use case.Beta Was this translation helpful? Give feedback.
All reactions