I'm trying to upgrade my typescript dependency (2.5.x > 3.1.3) in my React-Redux project. Sadly not without problems :P
I have a base selectpicker React component that expects a function property with a parameter of type IdNameObject:
onToggleItem: (item: IdNameObject) => void;
The actual function that gets injected into the function prop, is a Redux dispatch function with a parameter of an interface that extends the IdNameObject:
updateSelectedLocation: (location: Location) => void;
interface Location extends IdNameObject {...}
Typescript now throws an error saying that type Location is not equal to type IdNameObject, obviously.
I tried converting the function property to make it generic:
onToggleItem: <T extends IdNameObject>(item: T) => void
This however still throws a typescript error:
type '(location: Location) => void' is not assignable to type '<T extends IdNameObject>(item: T) => void'
Any idea what I should be doing in this case?
Full example case
I left out all the extra code that is not really needed for this case.
On one side I have a navigation.tsx:
interface Location extends IdNameObject {...}
interface Props {
updateSelectedLocation: (location: Location) => void;
}
class Navigation extends React.Component<Props> {
public render(): any {
const {updateSelectedLocation} = this.props;
return <Selectpicker onToggleItem={updateSelectedLocation}/>;
}
}
function mapDispatchToProps(dispatch: DispatchType) {
return {
updateSelectedLocation: (location: Location) => {
dispatch(updateSelectedLocation(location));
},
}
}
export default connect<StateProps, DispatchProps, OwnProps>(mapStateToProps, mapDispatchToProps)((Navigation as any));
On the other side i have a selectpicker.tsx:
interface Location extends IdNameObject {...}
interface Props {
onToggleItem: (item: IdNameObject) => void;
}
export class Selectpicker extends React.Component<Props> {
public render(): any {
const {onToggleItem} = this.props;
return <Dropdown contentOnToggleItem={onToggleItem}/>;
}
}