N
Nuxtβ€’9mo ago
Zack PlauchΓ©

useCookie typescript saying that Boolean should be a string, when it's a boolean

I have code that does:
const login() => {
const isLoggedIn = useCookie('isLoggedIn')
isLoggedIn.value = 'true'
}

definePageMeta({
middleware: () => {
const isLoggedIn = useCookie('isLoggedIn')
if (isLoggedIn.value === 'true') {
navigateTo('/page/')
}
}
})
const login() => {
const isLoggedIn = useCookie('isLoggedIn')
isLoggedIn.value = 'true'
}

definePageMeta({
middleware: () => {
const isLoggedIn = useCookie('isLoggedIn')
if (isLoggedIn.value === 'true') {
navigateTo('/page/')
}
}
})
Problem is, isLoggedIn.value actually returns a Boolean, not a string like TypeScript thinks πŸ™‚ So looks like something might be wrong with the docs for this πŸ‘€ How to fix it? Literally this works:
if (isLoggedIn.value === true) {...}
if (isLoggedIn.value === true) {...}
But typescript says it's wrong.
1 Reply
pyplacca
pyplaccaβ€’9mo ago
The return value of useCookie is typed as string | undefined | null by default. You can override that via its type argument
Want results from more Discord servers?
Add your server