Skip to content

TypeScript Error When Using CSS Variables in Props #1075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
michaltarasiuk opened this issue May 22, 2025 · 1 comment
Open

TypeScript Error When Using CSS Variables in Props #1075

michaltarasiuk opened this issue May 22, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@michaltarasiuk
Copy link

michaltarasiuk commented May 22, 2025

Describe the issue

I'm trying to pass styles as props in TypeScript, but I'm getting an error when I use a CSS variable. The error says that the type 'Color<...>' can't be assigned to 'Readonly<string | null | undefined>' in the style prop.

Expected behavior

Extend the type 'Readonly<string | null | undefined>' to include a CSS variable

Steps to reproduce

function Parent() {
  return <Child style={styles.base} />;
}

function Child({style}: {style: stylex.StyleXStyles}) {
  return <div {...stylex.props(style)} />;
}

const styles = stylex.create({
  base: {
    backgroundColor: baseColors.background,
  },
});

// tokens.stylex.ts
const baseColors = stylex.defineVars({
  background: stylex.types.color('#000000'),
});
@michaltarasiuk michaltarasiuk added the bug Something isn't working label May 22, 2025
@michaltarasiuk
Copy link
Author

michaltarasiuk commented May 22, 2025

It can be solved this way, but I'm not sure it's the best option.

function Parent() {
  return <Child style={styles.base} />;
}

function Child({
  style,
}: {
  style: stylex.StyleXStyles<{
    backgroundColor: Readonly<string | null | undefined> | stylex.Types.Color<string>;
  }>;
}) {
  return <div {...stylex.props(style)} />;
}

const styles = stylex.create({
  base: {
    backgroundColor: baseColors.background,
  },
});

// tokens.stylex.ts
const baseColors = stylex.defineVars({
  background: stylex.types.color('#000000'),
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant