🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Nextjs Landing Pages - TypeScript Error When Checking For Non-Page Types

6 replies
Last updated: Jul 31, 2022
I am changing the sanity-template-nextjs-landing-pages over to TypeScript.
One of the issues that I cannot resolve in my
file is this:
When checking for non-page types with
if(!data?._type === 'page')
, I get a TS error
This condition will always return 'false' since the types 'boolean' and 'string' have no overlap.
The value of
is always a string and I’m pretty sure
!foo === 'bar'
is the right syntax?
What’s the issue and how do I resolve it?
Jul 28, 2022, 4:13 AM
user M
, wonderful that you’re doing that 🙂 I hope you will share the final code with us all!
I will have a look at the issue and get back at you
Jul 28, 2022, 1:46 PM
Until Sanity v3 is closer to an RC (with better TypeScript support), I’m just focusing on the NextJS part of that project.
Jul 28, 2022, 10:58 PM
So you don’t need feedback on this now?
Jul 29, 2022, 6:32 PM
Because I got some TS answers for you :cool_doge:

if (data?._type !== 'page')
It was a bug in the original code and in ts it would always return
casts the string to a
, and
bool === string
will never be true. Removing the
will make it so it compares
Jul 29, 2022, 9:19 PM
Such a simple solution. I never thought of
. That is a simple option and something I could have attempted but never thought of! Thanks so much
user J
Jul 30, 2022, 2:46 AM
Our amazing team thought of it... 🙌 And pleasure 🙃
Jul 31, 2022, 9:44 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?