Skip to content

Better errors for JSX children #29251

Closed
@DanielRosenwasser

Description

@DanielRosenwasser

Today, error messages on JSX children are reported in a broad way - the type checker relies on reporting using the effective props given against the expected props.

import * as React from "react";

interface Props {
  children: (x: number) => string;
}

export function Blah(props: Props) {
  return <></>;
}

var a = <Blah>
  {x => x}
</Blah>

// ERROR:
// Type '{ children: (x: number) => number; }' is not assignable to type 'Props'.
//   Types of property 'children' are incompatible.
//     Type '(x: number) => number' is not assignable to type '(x: number) => string'.
//       Type 'number' is not assignable to type 'string'.

Instead, much like we report on individual elements of an array literal, we can try to report errors on individual child elements. Here's a few examples where we can specialize:

import * as React from "react";

interface Props {
  children: (x: number) => string;
}

export function Blah(props: Props) {
  return <></>;
}

// Incompatible child.
var a = <Blah>
  {x => x}
</Blah>

// Blah components don't accept text as child elements
var a = <Blah>
  Hello unexpected text!
</Blah>

// Blah components don't accept multiple children.
var a = <Blah>
  {x => "" + x}
  {x => "" + x}
</Blah>

Metadata

Metadata

Assignees

Labels

Domain: Error MessagesThe issue relates to error messagingDomain: JSX/TSXRelates to the JSX parser and emitterExperience EnhancementNoncontroversial enhancementsFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions