Here is the scenario:
I have a custom component:
class MyComponent extends React.Component {
render () {
return (
As of TypeScript 3.1, all JSX elements are hard-coded to have the JSX.Element
type, so there's no way to accept certain JSX elements and not others. If you wanted that kind of checking, you would have to give up the JSX syntax, define your own element factory function that wraps React.createElement
but returns different element types for different component types, and write calls to that factory function manually.
There is an open suggestion, which might be implemented as soon as TypeScript 3.2 (to be released in late November 2018), for TypeScript to assign types to JSX elements based on the actual return type of the factory function for the given component type. If that gets implemented, you'll be able to define your own factory function that wraps React.createElement
and specify it with the jsxFactory
compiler option, and you'll get the additional type information. Or maybe @types/react
will even change so that React.createElement
provides richer type information, if that can be done without harmful consequences to projects that don't care about the functionality; we'll have to wait and see.