Skip to content

Instantly share code, notes, and snippets.

@rjdestigter
Created November 29, 2017 01:20
Show Gist options
  • Save rjdestigter/55e6fbcd17b7e9b421e9f6e13f675a0b to your computer and use it in GitHub Desktop.
Save rjdestigter/55e6fbcd17b7e9b421e9f6e13f675a0b to your computer and use it in GitHub Desktop.
TypeScript React classes with generics throw Type '{}' is not assignable to type 'R'.
// Libs
import * as React from 'react'
// Types
interface Bar {
id: number
}
interface Party<R extends Bar> {
attr: keyof R
desc: string
}
interface Props<R extends Bar> {
property: Party<R>
}
// Exports
class Test<R extends Bar> extends React.Component<Props<R>> {
public props: Props<R>
public render() {
return null
}
}
export default class ImplementTest<R extends Bar> extends React.Component<Props<R>> {
public props: Props<R>
public render() {
return (
<Test property={this.props.property} />
)
}
}
@rjdestigter
Copy link
Author

Type '{ property: Party<R>; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Test<{}>> & Props<{}>'.
  Type '{ property: Party<R>; }' is not assignable to type 'Props<{}>'.
    Types of property 'property' are incompatible.
      Type 'Party<R>' is not assignable to type 'Party<{}>'.
        Type '{}' is not assignable to type 'R'.```

@rjdestigter
Copy link
Author

TypScript 2.6.1
"react": "^16.1.1",
"@types/react": "^16.0.25",```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment