Static types
Types for style props
StyleX comes with full support for Static Types. The most common utility type is
StyleXStyles
which is used to accept any arbitrary StyleX styles.
- Typescript
- Flow
import type {StyleXStyles} from '@stylexjs/stylex';
import * as stylex from '@stylexjs/stylex';
type Props = {
...
style?: StyleXStyles,
};
function MyComponent({style, ...otherProps}: Props): React.MixedElement {
return (
<div
{...stylex.props(localStyles.foo, localStyles.bar, style)}
>
{/* ... */}
</div>
);
}
import type {StyleXStyles} from '@stylexjs/stylex';
import * as stylex from '@stylexjs/stylex';
type Props = $ReadOnly<{
...
style?: StyleXStyles<>,
}>;
function MyComponent({style, ...otherProps}: Props): React.MixedElement {
return (
<div
{...stylex.props(localStyles.foo, localStyles.bar, style)}
>
{/* ... */}
</div>
);
}
StaticStyles
can be used instead of StyleXStyles
to accept arbitrary static
styles, but disallow dynamic styles.
Constraining accepted styles
Type arguments can be used with StyleXStyles<{...}>
to limit the styles that
are accepted.
Accepting from a set of style properties
To limit the accepted style properties to a given set, an object type with the allowed properties can be used:
- Typescript
- Flow
import type {StyleXStyles} from '@stylexjs/stylex';
type Props = {
// ...
style?: StyleXStyles<{
color?: string;
backgroundColor?: string;
borderColor?: string;
borderTopColor?: string;
borderEndColor?: string;
borderBottomColor?: string;
borderStartColor?: string;
}>;
};
import type {StyleXStyles} from '@stylexjs/stylex';
type Props = $ReadOnly<{
// ...
style?: StyleXStyles<{
color?: string;
backgroundColor?: string;
borderColor?: string;
borderTopColor?: string;
borderEndColor?: string;
borderBottomColor?: string;
borderStartColor?: string;
}>;
}>;
The style
prop will now accept only the properties defined but disallow
anything else.
It is a good practice to make the keys of the style types optional and have baseline styles in the component itself.
Typescript object types don’t error when given objects with extra properties. We’ve taken steps to mitigate this issue, but there may be edge-cases where you’ll be able to pass in extra, disallowed styles without a type error.
Limiting the possible values for styles
In addition to the accepted style properties, the values for those properties can be constrained too.
- Typescript
- Flow
import type {StyleXStyles} from '@stylexjs/stylex';
type Props = {
...
// Only accept styles for marginTop and nothing else.
// the value for marginTop can only be 0, 4, or 8.
style?: StyleXStyles<{
marginTop: 0 | 4 | 8 | 16
}>,
};
import type {StyleXStyles} from '@stylexjs/stylex';
type Props = $ReadOnly<{
...
// Only accept styles for marginTop and nothing else.
// the value for marginTop can only be 0, 4, or 8.
style?: StyleXStyles<{
marginTop: 0 | 4 | 8 | 16
}>,
}>;
Now, this component only accepts styles that have a marginTop
property and no
other properties. The value for marginTop
can only be one of 0
, 4
, 8
, or
16
.
Disallowing properties
It is sometimes more convenient to define a blocklist instead of an allowlist.
- Typescript
- Flow
import type {StyleXStylesWithout} from '@stylexjs/stylex';
import * as stylex from '@stylexjs/stylex';
type NoLayout = StyleXStylesWithout<{
postion: unknown,
display: unknown,
top: unknown,
start: unknown,
end: unknown,
bottom: unknown,
border: unknown,
borderWidth: unknown,
borderBottomWidth: unknown,
borderEndWidth: unknown,
borderStartWidth: unknown,
borderTopWidth: unknown,
margin: unknown,
marginBottom: unknown,
marginEnd: unknown,
marginStart: unknown,
marginTop: unknown,
padding: unknown,
paddingBottom: unknown,
paddingEnd: unknown,
paddingStart: unknown,
paddingTop: unknown,
width: unknown,
height: unknown,
flexBasis: unknown,
overflow: unknown,
overflowX: unknown,
overflowY: unknown,
}>;
type Props = {
// ...
style?: NoLayout,
};
function MyComponent({style, ...}: Props) {
return (
<div
{...stylex.props(localStyles.foo, localStyles.bar, style)}
>
{/* ... */}
</div>
);
}
import type {StyleXStylesWithout} from '@stylexjs/stylex';
import * as stylex from '@stylexjs/stylex';
type NoLayout = StyleXStylesWithout<{
postion: mixed,
display: mixed,
top: mixed,
start: mixed,
end: mixed,
bottom: mixed,
border: mixed,
borderWidth: mixed,
borderBottomWidth: mixed,
borderEndWidth: mixed,
borderStartWidth: mixed,
borderTopWidth: mixed,
margin: mixed,
marginBottom: mixed,
marginEnd: mixed,
marginStart: mixed,
marginTop: mixed,
padding: mixed,
paddingBottom: mixed,
paddingEnd: mixed,
paddingStart: mixed,
paddingTop: mixed,
width: mixed,
height: mixed,
flexBasis: mixed,
overflow: mixed,
overflowX: mixed,
overflowY: mixed,
}>;
type Props = $ReadOnly<{
// ...
style?: NoLayout,
}>;
function MyComponent({style, ...}: Props): React.MixedElement {
return (
<div
{...stylex.props(localStyles.foo, localStyles.bar, style)}
>
{/* ... */}
</div>
);
}
Here the listed properties in the object type will be disallowed, but all other styles will still be accepted.