I've encountered an error that I'm struggling to fully understand. It seems to be related to the fact that a certain value might be undefined, requiring me to check if it exists. However, when setting up the code as shown below, errors occur. Surprisingly, replacing {showDescription &&
with
{isFeatureBlock && description &&
inline solves the issue. But why doesn't it work when assigned to a const?
Note: This is a simplified version focusing on the main problem at hand.
The Code
isFeatureBlock
is a boolean and description
is an optional string
const showDescription = isFeatureBlock && description
{showDescription && (
<BlockDescription>
{description}
</BlockDescription>
)}
The Error
- 'Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'