Skip to content
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

The css prop is not attached to a type of props if it is a disjoint union type on className field. #3185

Closed
ENvironmentSet opened this issue May 14, 2024 · 0 comments · Fixed by #3232

Comments

@ENvironmentSet
Copy link
Contributor

ENvironmentSet commented May 14, 2024

Current behavior:

WithConditionalCSSProp<P> doesn't create the css prop if P is a disjoin union type on className field. Therefore we can't use some components optionally taking className field, like Link of wouter.

To reproduce:

Since it's not a problem inherent in emotion or react but a problem of treating union types, we can reproduce it with following simplified snippet.

type WithConditionalCSSProp<P> = 'className' extends keyof P
  ? string extends P['className' & keyof P]
    ? { css?: 'Interpolation<Theme>' } // To make the snippet simpler, I've just used string literal instead.
    : {}
  : {}

type BUG = WithConditionalCSSProp<{ className: string, foo: number } | { bar: boolean }> 

typescript playground

However, It's always good to have detailed reproduction. Here is the case I encountered.

image

Expected behavior:

I think it might be more accurate to add css prop to branches where proper className field is available. Therefore pass type check for components optionally taking className like wouter's.

I suggest following as a fix:

// Make LibraryManagedAttributes distributed over unions
type LibraryManagedAttributes<C, P> = P extends unknown ? 
  WithConditionalCSSProp<P> & ReactJSXLibraryManagedAttributes<C, P> 
  : never

Environment information:

  • react version: 18.2.0
  • @emotion/react version: 11.11.4
@ENvironmentSet ENvironmentSet changed the title The css prop is not attached to a type of props if it is a disjoin union type on className field. The css prop is not attached to a type of props if it is a disjoint union type on className field. Jul 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant