Skip to content

Types for Text component doesn't allow JavaScript expressions #166

@fnky

Description

@fnky

Describe the bug

When using JSX JavaScript expressions within a Text component, TypeScript will throw a type error:

<Text>There are {3} bananas in the bunch</Text>
 ^~~~

This JSX tag's 'children' prop expects a single child of type 'string | number | undefined', but multiple children were provided. ts(2746)

No overload matches this call.
  This JSX tag's 'children' prop expects a single child of type 'string | number | undefined', but multiple children were provided.
  Overload 2 of 2, '(props: PropsWithChildren<TextProps>, context?: any): any', gave the following error.
    Type '{ children: ReactText[]; }' is not assignable to type 'TextProps'.
      Types of property 'children' are incompatible.
        Type 'ReactText[]' is not assignable to type 'string | number | undefined'.
          Type 'ReactText[]' is not assignable to type 'string'. ts(2769)

To Reproduce
Steps to reproduce the behavior:

  1. Create a component
  2. Render a text component that contains a string with JavaScript expressions:
    <Text>There are {3} bananas in the bunch</Text>
  3. See type error.

Expected behavior
The Text component should accept JavaScript expressions, allowing children to be of string, number and an array of those.

The type of TextProps["children"] should be changed to React.ReactText | React.ReactText[] to allow JavaScript expressions.

Screenshots
If applicable, add screenshots to help explain your problem.

image

Desktop (please complete the following information):

  • OS: macOS Mojave
  • NodeGUI version: 0.15.2
  • React NodeGUI version: 0.5.0
  • OS Version: 10.14.5

Additional context
While you can put use string template literals, this won't be identical to how JSX handles text nodes with expressions. The children of a component using expressions should be an array of text nodes (Array<string | number>), ideally using React.ReactText.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions