Skip to content

Instantly share code, notes, and snippets.

@jtmthf
Created May 5, 2018 20:40
Show Gist options
  • Save jtmthf/282c77d1851879ce0046b5cf0cc30bcf to your computer and use it in GitHub Desktop.
Save jtmthf/282c77d1851879ce0046b5cf0cc30bcf to your computer and use it in GitHub Desktop.
// rendering a plain div
<Any />; // -> <div />
// using props defined on a div
<Any style={{ color: 'red' }} />; // -> <div style={{ color: 'red' }} />
// Declaring a new Input type
const Input = Any.ofType<'input'>();
// Failure to pass literal 'input' to is will be a type failure
<Input is="input" />;
// You can also now include input specific props. Including 'type' and 'value'
// would be a type failure with the div.
<Input is="input" type="text" value="text value" />;
// Using components
type BoxProps = {
color: string;
};
const StatelessBox: SFC<BoxProps> = ({ color }) => <div style={{ color }} />;
// When using `ofType` with functional components you need to use `typeof`.
const AnyStatelessBox = Any.ofType<typeof StatelessBox>();
// Failure to include both `StatelessBox` and `color` is a type error now.
<AnyStatelessBox is={StatelessBox} color="red" />;
class Box extends Component<BoxProps> {
public render() {
const { color } = this.props;
return <div style={{ color }} />;
}
}
// Testing with class components.
// Note that `typeof` is no longer required here.
const AnyBox = Any.ofType<Box>();
<AnyBox is={Box} color="red" />;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment