Skip to content

Better errors for JSX children #29251

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

Closed
DanielRosenwasser opened this issue Jan 3, 2019 · 2 comments · Fixed by #29264
Closed

Better errors for JSX children #29251

DanielRosenwasser opened this issue Jan 3, 2019 · 2 comments · Fixed by #29264
Assignees
Labels
Domain: Error Messages The issue relates to error messaging Domain: JSX/TSX Relates to the JSX parser and emitter Experience Enhancement Noncontroversial enhancements Fixed A PR has been merged for this issue

Comments

@DanielRosenwasser
Copy link
Member

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>
@DanielRosenwasser DanielRosenwasser added Domain: JSX/TSX Relates to the JSX parser and emitter Domain: Error Messages The issue relates to error messaging Experience Enhancement Noncontroversial enhancements labels Jan 3, 2019
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 3.4.0 milestone Jan 3, 2019
@weswigham
Copy link
Member

Uhhhh @DanielRosenwasser we may want to revise some of our jsx children checking rules while we update this. Some stuff we do seems real strange.

@DanielRosenwasser
Copy link
Member Author

Oh... uh, good! That sounds non-ominous.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Error Messages The issue relates to error messaging Domain: JSX/TSX Relates to the JSX parser and emitter Experience Enhancement Noncontroversial enhancements Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants