Spreading props in JSX on right hand side of && operator gives incorrect undefined warning
See original GitHub issueBug 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:
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (2 by maintainers)
Top Results From Across the Web
Issue with required prop and JSX prop spread #2124 - GitHub
But, only in that context, it incorrectly unifies undefined with other types. (Updated: to reflect that if you desugar any of the examples...
Read more >How To Customize React Components with Props
In this step, you will create a component that will change based on the input information called props. Props are the arguments you...
Read more >Unknown Prop Warning - React
The unknown-prop warning will fire if you attempt to render a DOM element with a prop that is not recognized by React as...
Read more >Optional chaining (?.) - JavaScript - MDN Web Docs - Mozilla
If the object accessed or function called is undefined or null, it returns undefined ... SyntaxError: Invalid left-hand side in assignment
Read more >React Best Practices – Tips for Writing Better React Code in ...
Two years ago, I started to learn and use React. And today I'm still using it at my day job as a Software...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Here’s a simplified example of what you’re trying to do:
Narrowing the value you stored in an object previously will not narrow the object.
@karlhorky See this comment, which in turn links to this comment chain.