Typescript: TSX: Can't use a component stored in a variable

Created on 12 Jan 2016  ·  3Comments  ·  Source: microsoft/TypeScript

Repro

Suppose I have a component called MyComponent.

Using MyComponent like this works:

render() {
  return <MyComponent />;
}

but storing MyComponent in a variable and then trying to use it does not:

render() {
  var anAlias = MyComponent;
  return <anAlias />;
}

It results in the following error:

error TS2339: Property 'anAlias' does not exist on type 'JSX.IntrinsicElements'.

Workaround

Instead of using TSX, use React.createElement:

render() {
  var anAlias = MyComponent;
  return React.createElement(anAlias);
}
By Design

Most helpful comment

anAlias is an intrinsic JSX element since it starts with lowercase letter. Intrinsic elements are looked up on JSX.IntrinsicElements interface and in your case it presumably does not have member anAlias. Try making it upper-case so it will be treated as value-based element.

All 3 comments

anAlias is an intrinsic JSX element since it starts with lowercase letter. Intrinsic elements are looked up on JSX.IntrinsicElements interface and in your case it presumably does not have member anAlias. Try making it upper-case so it will be treated as value-based element.

Vlad covered it -- this is the same behavior you'd see with the regular JSX transformer (lower-case identifiers are treated the same as div or span)

Thanks. I was not aware of the distinction between intrinsic and value-based elements and I didn't know that the casing of the first letter was significant.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

blakeembrey picture blakeembrey  ·  171Comments

kimamula picture kimamula  ·  147Comments

tenry92 picture tenry92  ·  146Comments

OliverJAsh picture OliverJAsh  ·  242Comments

quantuminformation picture quantuminformation  ·  273Comments