Skip to content

Instantly share code, notes, and snippets.

@patrick91
Created May 9, 2017 10:48
Show Gist options
  • Save patrick91/8e6f28bcaa7e2c8dbf8676c67ddb9426 to your computer and use it in GitHub Desktop.
Save patrick91/8e6f28bcaa7e2c8dbf8676c67ddb9426 to your computer and use it in GitHub Desktop.
import * as React from 'react';
import styled from 'styled-components';
import { ComponentClass, StatelessComponent } from 'react';
export type Component<P> = ComponentClass<P> | StatelessComponent<P>;
interface FooProps {
message: string,
primary: boolean,
};
interface NewComponentProps {
message: string,
};
const getTestComponent = ({ Foo }: { Foo: Component<FooProps> }): Component<NewComponentProps> => {
return class NewComponent extends React.Component<NewComponentProps, void> {
render() {
return (
<Foo message={this.props.message} primary />
)
}
}
}
// Autocompletes as expected and works
const NormalTestComponent = getTestComponent({
Foo: (props) => (
<div>{props.message}</div>
),
})
// Styled Components does not autocomplete on interpolation and doesnt work
const StyledTestComponent = getTestComponent({
Foo: styled.div`
background: ${(props: NewComponentProps) => props.primary ? 'red' : 'purple'};
`,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment