Using the sx prop with TypeScript
#1723
|
I've tried using the Is there additional configuration needed to use the Example: https://stackblitz.com/edit/github-exkhdnrp?file=src%2FApp.tsx |
Replies: 1 comment 1 reply
|
The StyleX transform can compile the For example, add something like import type { StyleXStyles } from '@stylexjs/stylex';
declare module 'react' {
interface HTMLAttributes<T> {
sx?: StyleXStyles;
}
interface SVGAttributes<T> {
sx?: StyleXStyles;
}
}Then this should type-check: import * as stylex from '@stylexjs/stylex';
const styles = stylex.create({
button: { color: 'red' },
});
export function App() {
return <button sx={styles.button}>Click</button>;
}The bundler plugin still has to run the StyleX transform; If you do not want a global JSX augmentation, the typed fallback is still the explicit form: <button {...stylex.props(styles.button)}>Click</button> |
The StyleX transform can compile the
sxshorthand, but TypeScript is still checking React's normal JSX types. React's intrinsicbutton/divprops do not includesx, so you need to add a local ambient type if you want to use that shorthand in TS.For example, add something like
src/stylex-jsx.d.tsand make sure it is included by yourtsconfig:Then this should type-check: