You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
React .tsx with Union Type Props reports error. But works in pure TS #26004
interfacePS{multi: falsevalue: string|undefinedonChange: (selection: string|undefined)=>void}interfacePM{multi: truevalue: string[]onChange: (selection: string[])=>void}exportfunctionComponentWithUnion(props: PM|PS){return"foo";}// Usage with React tsxexportfunctionHereIsTheError(){return(<ComponentWithUnionmulti={false}value={'s'}onChange={val=>console.log(val)}// <- this throws an error/>);}// Usage with pure TypeScriptComponentWithUnion({multi: false,value: 's',onChange: val=>console.log(val)// <- this works fine})
Expected behavior:
Both should infer the correct type and don't throw an error.
Actual behavior:
Throw's the following error:
Parameter 'val' implicitly has an 'any' type
Playground Link: https://codesandbox.io/s/w07omnr85k
In file index.ts at line 25 you can see that val is of type any. But in line 34 it correctly displays type string.
Activity
mattmccutchen commentedon Jul 27, 2018
Looks like the problem is that each JSX attribute is contextually typed independently. If you use a spread attribute, it does work:
Could the compiler just treat all the JSX attributes as if they were written in a single object literal?
pelotom commentedon Aug 14, 2018
This affects overloads as well as unions:
pelotom commentedon Aug 14, 2018
Also here's a case where wrapping the props in a JSX spread doesn't help: