Promise<UserCredential> 不可分配给类型

Promise<UserCredential> is not assignable to type

import React, {createContext, useContext, useEffect, useState, ReactNode} from "react";
import { auth } from '../utils/init-firebase';
import { createUserWithEmailAndPassword } from "firebase/auth"

type ButtonProps = {
    children: ReactNode;
    
}

export const useAuth = () =>useContext(AuthContext);

const AuthContext = createContext({
    currentUser: null,
    register: Promise,
})

export default function AuthContextProvider( { children}  : ButtonProps){
    const [currentUser, setCurrentUser] = useState(null);

    function register(email: string, password:string) {
        return createUserWithEmailAndPassword(auth, email, password)
    }
    
    const value = {
        currentUser,
        register,
    }
    
    return <AuthContext.Provider value={value}>
        {children}
    </AuthContext.Provider>
}

我遇到这样的错误:

Type '{ currentUser: null; register: (email: string, password: string) => Promise<UserCredential>; }' is not assignable to type '{ currentUser: null; register: PromiseConstructor; }'.
  Types of property 'register' are incompatible.
    Type '(email: string, password: string) => Promise<UserCredential>' is missing the following properties from type 'PromiseConstructor': all, race, reject, resolve, and 3 more.

如何解决这个问题?

改变

const AuthContext = createContext({
    currentUser: null,
    register: Promise,
})

const AuthContext = createContext({
    currentUser: null,
    register: (email: string, password: string) => Promise.resolve(),
})

问题是您正在使用 Promise class.

初始化寄存器

最好也输入上下文。要进行更改:

const AuthContext = createContext({
    currentUser: null,
    register: Promise,
})

至:

type AuthContextType = {
  currentUser: User | null; //I'm not sure what can be stored in User value, currently you have there only `null`
  //it's unclear what return type `createUserWithEmailAndPassword` function has. I gues it's something like UserCredential, basing on documentation: https://firebase.google.com/docs/reference/js/v8/firebase.auth.Auth#createuserwithemailandpassword  
  register: (email: string, password: string) => Promise<UserCredential | void>
}
const AuthContext = createContext<AuthContextType>({
    currentUser: null,
    register: (email: string, password: string) => Promise.resolve(),
})