admin管理员组文章数量:1199791
The editor is showing
Type 'Promise' is not assignable to type 'void | Destructor'.
for the checkUserLoggedIn() call in useEffect.
I can get rid of it by doing const checkUserLoggedIn:any
However maybe that's not the most ideal way of solving this...
If I do const checkUserLoggedIn:Promise I then get a different error:
This expression is not callable. Type 'Promise' has no call signatures.
which is not what I want...I want it to be callable...I'm translating/converting my javascript file to typescript.....
useEffect(() => checkUserLoggedIn(), [])
// Check if user is logged in
const checkUserLoggedIn = async () => {
console.log('checkUserLoggedIn')
const res = await fetch(`${NEXT_URL}/api/user`)
const data = await res.json()
if (res.ok) {
setUser(data.user)
console.log('data.user', data.user)
router.push('/account/dashboard')
} else {
setUser(null)
}
}
The editor is showing
Type 'Promise' is not assignable to type 'void | Destructor'.
for the checkUserLoggedIn() call in useEffect.
I can get rid of it by doing const checkUserLoggedIn:any
However maybe that's not the most ideal way of solving this...
If I do const checkUserLoggedIn:Promise I then get a different error:
This expression is not callable. Type 'Promise' has no call signatures.
which is not what I want...I want it to be callable...I'm translating/converting my javascript file to typescript.....
useEffect(() => checkUserLoggedIn(), [])
// Check if user is logged in
const checkUserLoggedIn = async () => {
console.log('checkUserLoggedIn')
const res = await fetch(`${NEXT_URL}/api/user`)
const data = await res.json()
if (res.ok) {
setUser(data.user)
console.log('data.user', data.user)
router.push('/account/dashboard')
} else {
setUser(null)
}
}
Share
Improve this question
asked May 13, 2022 at 18:42
prestonpreston
4,33710 gold badges54 silver badges89 bronze badges
0
2 Answers
Reset to default 18Arrow functions written in the way you've described implicitly return the value from the function. For example, this function () => 4
returns the value 4
. Conversely, when you wrap this in a block, you must explicitly define the return value: () => { 4; }
this function does not return anything.
In your case, you are passing an anonymous arrow function that implicitly returns the result of an asynchronous function, which is a promise.
To fix this, wrap the content of the function you pass to useEffect
in a block:
useEffect(() => {
checkUserLoggedIn();
}, []);
useEffect(() => {
checkUserLoggedIn()
}, [])
// Check if user is logged in
const checkUserLoggedIn:() => Promise<void> = async () => {
console.log('checkUserLoggedIn')
const res = await fetch(`${NEXT_URL}/api/user`)
const data = await res.json()
if (res.ok) {
setUser(data.user)
console.log('data.user', data.user)
router.push('/account/dashboard')
} else {
setUser(null)
}
}
版权声明:本文标题:javascript - Typescript: Type 'Promise<void>' is not assignable to type 'void | Destructor 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738528098a2093061.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论