Skip to content

[Question] Accept only one child of specific set of types #271

Closed
@PedroBern

Description

@PedroBern

Question

How to have a component that accepts only one child, of a specific set of types?

What I need

To accept one child of one of the following types (React Native):

  • Animated.FlatList
  • Animated.SectionList
  • Animated.ScrollView

What I have tried

Attempt 1

type ChildrenProps =
  | Animated.AnimatedProps<ScrollView>
  | Animated.AnimatedProps<SectionList>
  | Animated.AnimatedProps<FlatList>
  | SectionList['props']
  | FlatList['props']
  | ScrollView['props']

interface Props {
  children: React.ReactElement<ChildrenProps>
}

const MyComponent: React.FunctionComponent<Props> ...

Attempt 2

interface Props { 
children:
    | React.ReactElement<Animated.AnimatedComponent<typeof ScrollView>>
    | React.ReactElement<Animated.AnimatedComponent<typeof SectionList>>
    | React.ReactElement<Animated.AnimatedComponent<typeof FlatList>>
}

const MyComponent: React.FunctionComponent<Props> ...

Result

Either way, my component ends up accepting any kind of Children.

Bonus

If this is solved, how would I have a new prop, that accepts prop of the specific type? For example:

interface Props { 
children: ... // above
childrenProps: ... // (?) if FlatList, accept only props of FlatList, if ScrollView...
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions