Title: Suggestion: type checking for JSX children · Issue #13618 · microsoft/TypeScript · GitHub
Open Graph Title: Suggestion: type checking for JSX children · Issue #13618 · microsoft/TypeScript
X Title: Suggestion: type checking for JSX children · Issue #13618 · microsoft/TypeScript
Description: Rationale Props are an important part of a React component's interface, but children can be as well. In applications it's not uncommon to want to restrict what kind of children a component will accept. Currently this enforcement has to b...
Open Graph Description: Rationale Props are an important part of a React component's interface, but children can be as well. In applications it's not uncommon to want to restrict what kind of children a component will acc...
X Description: Rationale Props are an important part of a React component's interface, but children can be as well. In applications it's not uncommon to want to restrict what kind of children a component ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/13618
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Suggestion: type checking for JSX children","articleBody":"## Rationale\r\nProps are an important part of a React component's interface, but children can be as well.\r\nIn applications it's not uncommon to want to restrict what kind of children a component will\r\naccept. Currently this enforcement has to be done at runtime by throwing errors.\r\n\r\n## The Goal\r\nComponents can specify allowed children as a union type. Each member would correspond to\r\nthe tag, or 'type' field, of the resulting Element. Ideally this could be plucked from\r\nthe type of props.children, which would satisfy both component classes and SFCs.\r\n\r\n## Motivating Examples:\r\nA modal component may want to make assumptions about its children to satisfy layout\r\nconstraints.\r\n```jsx\r\ninterface IModalProps {\r\n children: ModalHeader | ModalBody | ModalFooter;\r\n}\r\n\r\nfunction Modal({ children }: IModalProps) { ... }\r\nfunction ModalHeader(props) { ... }\r\nfunction ModalBody(props) { ... }\r\nfunction ModalFooter(props) { ... }\r\n\r\n\u003cModal>\r\n \u003cModalHeader />\r\n \u003cdiv /> { /* Desired error: type 'div' does not exist in type 'ModalHeader | ModalBody | ModalFooter' */ }\r\n \u003cModalBody />\r\n \u003cModalFooter />\r\n\u003c/Modal>\r\n```\r\n\r\nSimilarly...\r\n```jsx\r\ninterface IButtonGroupProps {\r\n children: 'button';\r\n}\r\n\r\nfunction ButtonGroup({ children }: IButtonGroupProps) { ... }\r\n\r\n\u003cButtonGroup>\r\n \u003cbutton />\r\n \u003cbutton />\r\n \u003ca href='' /> { /* Desired error: type 'a' is not assignable to type 'button' */ }\r\n\u003c/ButtonGroup>\r\n```\r\n\r\nAn interesting emerging pattern is using JSX as a function call to turn imperative APIs into\r\ndeclarative ones. Currently these 'render callbacks' can't be type-checked at all. A more complete\r\nsummary can be found at http://reactpatterns.com/#render-callback. A further example can be found at https://github.com/ReactTraining/react-media#usage.\r\n```jsx\r\ninterface IUser {\r\n Name: string;\r\n}\r\n\r\ninterface IFetchUserProps {\r\n children(user: IUser): any;\r\n}\r\n\r\nclass FetchUser extends React.Component\u003cIFetchUserProps, any> {\r\n render() {\r\n return this.state\r\n ? this.props.children(this.state.result)\r\n : null;\r\n }\r\n\r\n componentDidMount() {\r\n api.fetchUser().then(result => this.setState({ result }));\r\n }\r\n}\r\n\r\nfunction UserName() {\r\n return (\r\n \u003cFetchUser>\r\n { user => (\r\n \u003ch1>{ user.NAme }\u003c/h1> // Desired error: property 'NAme' does not exist on type 'IUser'\r\n ) }\r\n \u003c/FetchUser>\r\n );\r\n}\r\n```\r\n\r\nLastly, I don't *think* any of this is necessarily specific to React. Other libraries leveraging the JSX\r\nspecification for component structure should be able to leverage this as well. The crux of this lies at\r\nthe relationship between the type of the 'children' attribute of a given tag and the children actually\r\npassed to it at usage sites.\r\n","author":{"url":"https://github.com/jwbay","@type":"Person","name":"jwbay"},"datePublished":"2017-01-21T18:56:26Z","url":"https://github.com/microsoft/TypeScript/issues/13618"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:4103535f-f753-4bf3-7bac-7338ed1a4473 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C8AE:4BB79:B00869:E2204C:6B00F94E |
| html-safe-nonce | 8170983d0c38ff5c6a81c578abe0f1b7d43a8e478cb459ed04524ff19659d4e1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDOEFFOjRCQjc5OkIwMDg2OTpFMjIwNEM6NkIwMEY5NEUiLCJ2aXNpdG9yX2lkIjoiODkxNzI5MTQ5NDk0ODI3MjQ2MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 29b724d416f24c6c90097133374a59947e9b7c637bcecf5492c04fdad61a2271 |
| hovercard-subject-tag | issue:202333206 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/13618/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9c4508fbd336ca30999e4e53209f687f89fc373ac6898d0229327221f8ad4477/microsoft/TypeScript/issues/13618 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9c4508fbd336ca30999e4e53209f687f89fc373ac6898d0229327221f8ad4477/microsoft/TypeScript/issues/13618 |
| og:image:alt | Rationale Props are an important part of a React component's interface, but children can be as well. In applications it's not uncommon to want to restrict what kind of children a component will acc... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jwbay |
| hostname | github.com |
| expected-hostname | github.com |
| None | e208d86428ec00a57ade75058166297440e8128a01e5287c31a53dc042bbf187 |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7dafd017fb7809a839c443dba08622af38e63c09 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width