TypeScript error with `!data?._type === 'page'` condition in Next.js slug file
This is actually a JavaScript/TypeScript operator precedence issue, not a Sanity-specific problem. The error message is telling you exactly what's wrong!
The issue is with your operator precedence. When you write:
if(!data?._type === 'page')JavaScript evaluates this as:
if((!data?._type) === 'page')So it's first negating data?._type with the ! operator (which gives you a boolean), and then comparing that boolean to the string 'page'. That's why TypeScript is complaining about comparing a boolean to a string.
The fix: You need to negate the entire comparison, not just the left side. Use parentheses:
if(!(data?._type === 'page'))Or more idiomatically, use the inequality operator:
if(data?._type !== 'page')The second option is cleaner and more commonly used in TypeScript/JavaScript code.
For your Next.js landing pages template, you'll also want to make sure you're properly typing your data object. If you're using Sanity's TypeGen (which generates TypeScript types from your schema), you can import the generated types and use them to get better type safety throughout your component. This will help catch these kinds of issues earlier and give you better autocomplete in your editor.
Show original thread6 replies
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.