question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Spreading props in JSX on right hand side of && operator gives incorrect undefined warning

See original GitHub issue

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

https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAJQKYEMDG8BmUIjgIilQ3wG4AocmATzCTgGFdIA7JFmAeTYAUcwAznAC8cAN7k4cGMBgAbJAC44AmFGAsA5hQC+FcmggtVjZkfZc2IuAAox02Qrg7lTcOY7ckfCIICUIgB84pJwRDAArlAscAA8ABYAjIFiMvJIOrEA9EmBuvo0dKbubBwAKgDuED6C1hJSaQqVEAD8yqrqWvmUhsbwbqwWzdZ2DunDLsWD5VU1AgHCwfVwvSaGJRZec3WhDY5KY01VADSheqHhUTGNSMMAZHdxAx6W9GIAdJ-r069zOnBZPLkc7kJAAD0gsDgABMkJgUBE5P0zKUYM0KFksnAALLUFYQWFwTQQJBCeJIIhAA

💻 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

image

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:

image

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
MartinJohnscommented, Mar 18, 2022

Here’s a simplified example of what you’re trying to do:

const value = undefined as string | undefined
const obj = { value }
if (value) {
  const expected: string = obj.value
}

Narrowing the value you stored in an object previously will not narrow the object.

1reaction
fatcerberuscommented, Sep 23, 2022

@karlhorky See this comment, which in turn links to this comment chain.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found