Bug Report
🔎 Search Terms
react child.props unknown
ts 18046 reading child prop
typescript user defined type guard fails after second time
🕗 Version & Regression Information
Upgrading project to 4.9.4 from 4.2.4.
- This changed between versions 4.7 and 4.8.2. I went through until I found where this changed.
⏯ Playground Link
Playground link with relevant code
💻 Code
importReactfrom'react';constchildArray=[]asanyasReact.ReactNode[];childArray.forEach(child=>{constchildren=React.isValidElement(child)&&child.props.children;// child.props is of type "unknown" error. If we swap this line with// the one above it, the error moves to the other line of code. Only// the line where we read child.props does it correctly type the value.// Multiple reads of child.props on the first line also work fine.constpropsValue=React.isValidElement(child)&&child.props.value;});// Wrapping the reads with one call to isValidElement will type correctlychildArray.forEach(child=>{if(React.isValidElement(child)){// no errors hereconstchildren=child.props.children;constpropsValue=child.props.value;}});childArray.forEach(child=>{constisValid=React.isValidElement(child);if(isValid){constchildren=child.props.children;constpropsValue=child.props.value;// after reading the isValid value a second time, child.props is // again unknown}elseif(isValid){constchildren=child.props.children;constpropsValue=child.props.value;}});🙁 Actual behavior
All examples fail after the second read of a type guard on props, whether re-running the function or just reading the variable.
🙂 Expected behavior
Should behave consistently no matter if this is the first or second read of the type guard.
Bug Report
🔎 Search Terms
react child.props unknownts 18046 reading child proptypescript user defined type guard fails after second time🕗 Version & Regression Information
Upgrading project to 4.9.4 from 4.2.4.
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
All examples fail after the second read of a type guard on
props, whether re-running the function or just reading the variable.🙂 Expected behavior
Should behave consistently no matter if this is the first or second read of the type guard.