I'm attempting to access the current theme colors in a custom React component. The example I'm using is from Material UI http://www.material-ui.com/#/customization/themes
I've tried a number of variations, but cannot get Typescript to compile my component. The error I get is... error TS2345: Argument of type 'typeof MainHeader' is not assignable to parameter of type 'Component<{}, {}>'.
import * as React from "react";
import muiThemeable from 'material-ui/styles/muiThemeable';
interface Properties {
title: string
}
class MainHeader extends React.Component<Properties, any> {
render() {
return (
<div className='hero' >
<div className='logo' />
<div className="bar" />
<h1>{this.props.title}</h1>
</div>
);
}
}
export default muiThemeable()(MainHeader);
it seems to me the material-ui definition for muiThemeable function is not right ...
doing
is the same as doing
and doesn't compile because you omitted component property type and component property state
the compiler infers
which doesn't match the Function constraints
... lets add the missing constrains
to satisfy the compiler , we should do
but this is giving an instance to the function , when the F is expecting a class
if you later on do ...
it won't work
but if you change muiThemeable definition to:
then you can use:
the tsc will generate errors
but it will transpile the right thing , and work
but that's not OK,
because:
it looks like its returning an instance when we need a class,... a type
finally:
this signature makes a bit more sense:
but after looking at the source
And this could be a way to get away with re-writing or augmenting the definition.
Wrapping the function
... and perhaps using a decorator to reduce boilerplate and easier reading...