Closed as not planned
Closed as not planned
Description
Bug Report
π Search Terms
Spread, Spreading, &&
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about prop spreading.
β― Playground Link
π» Code
import React from "react";
type ComponentOneProps = {
title: string;
};
const ComponentOne = ({ title }: ComponentOneProps) => {
return <h1>{title}</h1>;
};
type ComponentTwoProps = {
titleTwo?: string;
};
const ComponentTwo = ({ titleTwo }: ComponentTwoProps) => {
const componentOneProps = {
title: titleTwo,
};
return titleTwo && <ComponentOne {...componentOneProps} />;
};
export default ComponentTwo;
π Actual behavior
Because we checking that titleTwo
is truthy here (as it's the first operand of the logical AND operator) - the title
prop of ComponentOne
can never actually be set to undefined
, but we get a warning saying type Type 'string | undefined' is not assignable to type 'string'.
π Expected behavior
I would expect no error here, as the title
prop can never be actually set to a falsey value on ComponentOne
there. I think this may be an issue with spreading the props onto ComponentOne
here as it works if you set the title
prop directly: