Skip to content

Generic JSX props don't work properly when props contain generic functions #20891

Closed
@alshain

Description

@alshain

In 2.5.3, I was using JSX components with generic props containing functions successfully by wrapping stateful ones with stateless components, as also described in #6395 and in this comment

However, in 2.6.x and typescript@next, it breaks:

  Type '{ a: T; b: T[]; c: (t: T) => number; }' is not assignable to type 'Readonly<Props<{}>>'.
    Types of property 'c' are incompatible.
      Type '(t: T) => number' is not assignable to type '(t: {}) => number'.
        Types of parameters 't' and 't' are incompatible.
          Type '{}' is not assignable to type 'T'.
src/bug.tsx(32,18): error TS2322: Type '{ a: number; b: number[]; c: (x: number) => number; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Comp2<{}>> & Readonly<{ children?: ReactNode; }> &...'.
  Type '{ a: number; b: number[]; c: (x: number) => number; }' is not assignable to type 'Readonly<Props<{}>>'.
    Types of property 'c' are incompatible.
      Type '(x: number) => number' is not assignable to type '(t: {}) => number'.
        Types of parameters 'x' and 't' are incompatible.
          Type '{}' is not assignable to type 'number'.

TypeScript Version: 2.7.0-dev.20171224, 2.6.2

Code

import * as React from 'react';

interface Props<T> {
  a: T
  b: T[]
  c: (t: T) => number // This addition breaks it
}


class Comp2<T> extends React.Component<Props<T>> {
  render(){
    return null
  }
}


function Comp1<T>(props: Props<T>) {
    return <Comp2 {...props} /> // Error
}
  

const props = {
  a: 1,
  b: [2],
  c: (x: number) => x
}

const a = <Comp1 {...props} />

const b = <Comp2 {...props} /> // Same error

Expected behavior:

I expect it to work. It worked on 2.5.3

Actual behavior:

It breaks as soon as I add generic functions to the props

Config used:

{
    "compilerOptions": {
      "outDir": "build/dist",
      "module": "commonjs",
      "target": "es5",
      "lib": ["es6", "dom"],
      "sourceMap": true,
      "allowJs": true,
      "jsx": "react",
      "moduleResolution": "node",
      "rootDir": "src",
      "forceConsistentCasingInFileNames": true,
      "noImplicitReturns": true,
      "noImplicitThis": true,
      "noImplicitAny": true,
      "strictNullChecks": true,
      "suppressImplicitAnyIndexErrors": true,
      "experimentalDecorators": true,
      "emitDecoratorMetadata": true,
      "strict": true
      //"noUnusedLocals": true
    },
    "exclude": [
      "node_modules",
      "build",
      "scripts",
      "acceptance-tests",
      "webpack",
      "jest",
      "src/setupTests.ts"
    ],
    "types": [
      "typePatches"
    ]
  } 

The example works as expected on 2.5.3.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions