Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing number of children in TSX #22759

Closed
methyl opened this issue Mar 21, 2018 · 3 comments
Closed

Typing number of children in TSX #22759

methyl opened this issue Mar 21, 2018 · 3 comments
Assignees
Labels
Committed The team has roadmapped this issue Suggestion An idea for TypeScript

Comments

@methyl
Copy link

methyl commented Mar 21, 2018

Imagine a component which requires some exact number of children passed.

Can it be currently expressed in TypeScript? Tuple doesn't work here.

Code

import React from 'react'

interface ResizablePanelProps {
  children: [React.ReactNode, React.ReactNode]
}

class ResizablePanel extends React.Component<
  ResizablePanelProps, any> {}

const test = <ResizablePanel>
  <div />
  <div />
</ResizablePanel>

Expected behavior:
Pass.

Actual behavior:

Type '{ children: Element[]; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<ResizablePanel> & Readonly<{ children?: ReactNode;...'.
  Type '{ children: Element[]; }' is not assignable to type 'Readonly<ResizablePanelProps>'.
    Types of property 'children' are incompatible.
      Type 'Element[]' is not assignable to type '[ReactNode, ReactNode]'.
        Property '0' is missing in type 'Element[]'.

Code

import React from 'react'

interface ResizablePanelProps {
  children: [React.ReactNode, React.ReactNode]
}

class ResizablePanel extends React.Component<
  ResizablePanelProps, any> {}

const test = <ResizablePanel>
  <div />
  <div />
  <div />
</ResizablePanel>

Expected behavior:
Fail, provided 3 nodes instead of 2.

Playground Link:
https://stackblitz.com/edit/react-ts-8tttua

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Mar 21, 2018
@RyanCavanaugh
Copy link
Member

@weswigham I think we can just force tuple inference for the JSX children array and this should work automagically. Thoughts?

@weswigham
Copy link
Member

At least it should be doable to make the tupleness propegate via contextual type like it does elsewhere.

@methyl
Copy link
Author

methyl commented Oct 5, 2018

@weswigham amazing, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Committed The team has roadmapped this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants