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 disjoin union type on className field. #3185

Open
ENvironmentSet opened this issue May 14, 2024 · 0 comments

Comments

@ENvironmentSet
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant