I am using useContext for authentication purposes and my code is of below
type userSignup = {
name: string;
email: string;
password: string;
signup() => void;
}
export const AuthContext = createContext<userSignup|null>(null);
export const AuthProvider = ({children}) =>{
const signup = async (name,email,password) => {
const DTO ={name:name, email:email:, password:password}
const response = await fetch('url',DTO);
const data = await response.json();
setToken(data);
}
}
return (
<AuthContext.Provider value={{signup}}> {children} </AuthContext.Provider>
);
The issue is an error appears telling me that type (name: string, email: string,password: string, signup() => Promise) is not assignable to type userSignup.
I can overcome this by changing the
export const AuthContext = createContext<any|null>(null);
but I don't think using any is a good approach.
Could someone please suggest a better solution for this? Thank you in advance
//UPDATE 1
As David Alvarez suggested. I have changed the type userSignUp into:
type userSignUp ={
name: string;
email: string;
password: password;
signup: () => Promise<void>;
}
Now the error message says:
Type'(name:any, email:any, password:any) => Promise<void>' is not assignable to type '()=> Promise<void>'
It is in the compiler error :)
signup() => Promiseis not assignable tosignup() => void.According to the docs:
Change your type from
to
And it should work.