I'm still relatively new to typescript and I cannot figure out how to add types to a function that use a Promise.
I follow the suggestion from Typescript and make my function Props from UserStateProps to Promise<UserStateProps> but now is complaining that this Promise<UserStateProps> is missing the necessary properties.
I have this function:
const mergeCookiesWithUserState = async (defaultUserState: UserStateProps): Promise<UserStateProps> => {
const newUserState = { ...defaultUserState }
for (const key in newUserState) {
if (Object.getOwnPropertyDescriptor(cookiesName, key)) {
const cookieValue = await getCookieValue(cookiesName[key])
console.log({ cookieValue})
if (cookieValue !== undefined) {
newUserState[key] = cookieValue
}
}
}
return newUserState
}
which I use to update the React state.
The react user state uses UserStateProps to check the state types.
Since when I introduced a Promise in the mergeCookiesWithUserState, the user state complained about its props:
Type 'Promise<UserStateProps>' is missing the following properties from type 'UserStateProps': numOfSearches, language, adultContentFilter, openInNewTab, isModalOpents(2739)
These are the interface:
export interface UserStateProps {
numOfSearches: number
language: number
adultContentFilter: number
openInNewTab: boolean
isModalOpen: boolean
}
export interface UserContextProps {
userState: UserStateProps
setUserState: (userState: Partial<UserStateProps>) => void
}
Anyone can help me out to understand this thing?
UPDATE:
it turned out that the problem was how I have written getCookieValue() and therefore how to handle the returned Promised from it.
But all your comments put me on the right track again to figure out the solution.
I believe the answer I sign as correct, is the most pertinent one for the error I got and the async nature of my problem.
Thanks!