Skip to content

Commit 141b090

Browse files
committed
Add TypeScript types for sx prop
1 parent 20ce0b6 commit 141b090

4 files changed

Lines changed: 46 additions & 2 deletions

File tree

packages/@stylexjs/stylex/src/stylex.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import type {
2626
StyleX$DefineConsts,
2727
StyleXArray,
2828
StyleXClassNameFor,
29+
StyleXSxProp,
2930
StyleXStyles,
3031
StyleXStylesWithout,
3132
StyleXVar,
@@ -51,6 +52,7 @@ export type {
5152
StaticStylesWithout,
5253
StyleXArray,
5354
StyleXClassNameFor,
55+
StyleXSxProp,
5456
StyleXStyles,
5557
StyleXStylesWithout,
5658
StyleXVar,

packages/@stylexjs/stylex/src/types/StyleXTypes.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,20 @@ export type StyleXStylesWithout<CSS extends UserAuthoredStyles> = StyleXStyles<
235235
Omit<CSSPropertiesWithExtras, keyof CSS>
236236
>;
237237

238+
export type StyleXSxProp = StyleXArray<
239+
| null
240+
| undefined
241+
| boolean
242+
| CompiledStyles
243+
| Readonly<[CompiledStyles, InlineStyles]>
244+
>;
245+
246+
declare module 'react' {
247+
interface DOMAttributes<T> {
248+
sx?: StyleXSxProp;
249+
}
250+
}
251+
238252
declare const StyleXVarGroupTag: unique symbol;
239253
export type VarGroup<
240254
Tokens extends { [key: string]: any },

packages/@stylexjs/stylex/src/types/StyleXTypes.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ export type StyleXStylesWithout<
195195
out CSS extends { readonly [string]: unknown },
196196
> = StyleXStyles<Omit<CSSPropertiesWithExtras, keyof CSS>>;
197197

198+
export type StyleXSxProp = StyleXArray<
199+
?CompiledStyles | boolean | Readonly<[CompiledStyles, InlineStyles]>,
200+
>;
201+
198202
export type VarGroup<
199203
out Tokens extends { readonly [string]: unknown },
200204
out _ID extends string = string,

packages/typescript-tests/src/test1.tsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
*
88
*/
99

10-
/* eslint-disable no-unused-vars */
10+
/* eslint-disable no-unused-vars, react/no-unknown-property */
1111

1212
import * as React from 'react';
1313
import * as stylex from '@stylexjs/stylex';
14-
import type { StaticStyles } from '@stylexjs/stylex';
14+
import type { StaticStyles, StyleXSxProp } from '@stylexjs/stylex';
1515

1616
type Props = {
1717
xstyle?: StaticStyles;
@@ -21,16 +21,40 @@ function Component({ xstyle }: Props) {
2121
return <div {...stylex.props(xstyle)} />;
2222
}
2323

24+
function PlainComponent(_props: {}) {
25+
return null;
26+
}
27+
2428
const styles = stylex.create({
2529
base: {
2630
color: 'red',
2731
},
2832
});
2933

34+
const sxStyle: StyleXSxProp = styles.base;
35+
3036
function OtherComponent() {
3137
return <Component xstyle={styles.base} />;
3238
}
3339

3440
function OtherComponent2() {
3541
return <Component xstyle={[styles.base, undefined]} />;
3642
}
43+
44+
function SxComponent() {
45+
return <div sx={styles.base} />;
46+
}
47+
48+
function SxComponentMultiStyle() {
49+
return <div sx={[styles.base, undefined]} />;
50+
}
51+
52+
function InvalidSxValue() {
53+
// @ts-expect-error - `sx` only accepts StyleX styles.
54+
return <div sx="not-stylex" />;
55+
}
56+
57+
function CustomComponentSx() {
58+
// @ts-expect-error - `sx` is only transformed on lowercase JSX host elements.
59+
return <PlainComponent sx={styles.base} />;
60+
}

0 commit comments

Comments
 (0)