Multi-parameter React function components should be errors #33104
Labels
Experience Enhancement
Noncontroversial enhancements
Fix Available
A PR has been opened for this issue
Milestone
React function components take a single
props
parameter. If you mistakenly think they take positional parameters, the error message you get can be quite confusing because it says nothing about the number of parameters. Instead, it's usually something to do with assignability.Even if this is WAI, maybe this helps someone else with this error!
TypeScript Version: 3.5.2
Search Terms:
Code
Expected behavior:
It should be an error to use a function which takes multiple parameters as a component in JSX. This is almost certainly a mistake. It's similar to calling a two-parameter function with one parameter, which is an error:
Actual behavior:
No error.
b
andc
areundefined
at runtime.Playground Link: link
Related Issues:
Real-world example of the confusing way in which this came up:
Refactor
createCard
to be a function component:You get the following error message on the
row=
:It's not clear at first glance why the type checker is trying to assign
RowType
tostring | number
. If you squint hard enough, you eventually realize thatFunctionCard
is getting called with a single argument:TypeScript tries to assign this
{row, column}
object to the first parameter ofFunctionCard
, namelyrow
. Since this isRowType
, which has an index signature, it tries to assignthis.props.row
(whose type isRowType
) to the value type of the index signature, namelystring | number
. Hence the error.The text was updated successfully, but these errors were encountered: