Skip to content

Generic JSX Inference Issue #27411

Closed
Closed
@quicksnap

Description

@quicksnap

Tried finding an issue for this, but nothing turned up. I'm getting unexpected behavior with generic JSX:

TypeScript Version: 3.1.1

Search Terms: generic jsx inference

Code

import React, { Component } from 'react';

type Props<T> = {
  generate: (someArg: any) => T;
  consume: (data: T) => React.ReactNode;
};

class Foo<T> extends Component<Props<T>> {}

const fine = (
  <Foo
    generate={() => 123}
    consume={num => num / 2} // Works fine. Inferred type is `number`
  />
);

const error = (
  <Foo
    generate={thisCausesIssues => 123}
    consume={num => num / 2} // Error! Inferred type of `num` is `{}`
  />
);

Expected behavior:

num would infer correctly.

Actual behavior:

num is widened to the {} type. On TS 3.1.1 I get the error above. On ts@next, it seems not to understand my props whatsoever.

Playground Link:

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions