我希望以正确的方式定义接口,但遇到了问题,因为它期望有一个参数,即使参数为空也是如此。
我正在使用useContext
,并像这样定义接口:
//react-auth0-spa.tsx
interface Auth0Context {
......
loginWithRedirect: () => void; //I know this is the problem but I don't know what to define.
......
}
在提供程序中,
value
是指: //react-auth0-spa.tsx
<Auth0Context.Provider
value=({
....
loginWithRedirect: (...p: string[]) => auth0Client.loginWithRedirect(...p)
....
})
>
现在,我像这样将上下文导入到其他文件中:
const { isAuthenticated, loginWithRedirect, logout } = useAuth0()
这就是错误发生的地方。
///components/NavBar.tsx
const { isAuthenticated, loginWithRedirect, logout } = useAuth0()
<div>
{!isAuthenticated && (
<button onClick={() => loginWithRedirect({})}>Log in</button> //Expected 0 arguments, but got 1
)}
</div>
所以问题在于
loginWithRedirect
方法需要一个参数,但是传入的是一个空对象 () => loginWithRedirect({})
。可以通过在接口中使用 any
类型来解决该问题,但如果我想明确定义参数类型应该填什么呢?我尝试过
loginWithRedirect: ({}) => void
,但现在 loginWithRedirect: (...p: string[]) => auth0Client.loginWithRedirect(...p)
报错:Type '{}' is not assignable to type 'string'.ts(2322)
。源代码不包含 TypeScript,请访问以下链接:https://github.com/auth0-samples/auth0-react-samples/tree/master/01-Login/src。
请帮忙解决。
loginWithRedirect
可以使用什么类型的参数进行调用? - Ali FarisloginWithRedirect: () => void;
,以便我可以使用<button onClick={() => loginWithRedirect({})}>
。这意味着我需要在接口中提供一个参数,但我不知道该如何实现。 - bradrarloginWithRedirect: any;
并且它可以正常工作。问题是,我需要明确地定义它。我面临的问题是,如果参数为空,如何在接口中定义一个参数?<button onClick={() => loginWithRedirect({})}
- bradrar