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

[TS] From @types/react 16.3.9 React.CSSProperties is strongly typed #11004

Closed
xaviergonz opened this issue Apr 12, 2018 · 5 comments
Closed

[TS] From @types/react 16.3.9 React.CSSProperties is strongly typed #11004

xaviergonz opened this issue Apr 12, 2018 · 5 comments

Comments

@xaviergonz
Copy link
Contributor

xaviergonz commented Apr 12, 2018

From types/react 16.3.9 react now uses csstypes as a package for CSSProperties, which is strongly typed
This means that types for themes that used to rely on the fallback [k: string]: any for stuff such as "&:hover" gives now errors.

For those cases it should use something like

type ExtendedCSSProperties = React.CSSProperties & {
  [k: string]: any
}
@xaviergonz
Copy link
Contributor Author

Actually to get even better typing something as lame as this (typescript cannot make circular types :( ) would be preferred

type ExtendedCSSProperties = React.CSSProperties & {
  [k: string]: React.CSSProperties & {
    [k: string]: React.CSSProperties & {
      [k: string]: React.CSSProperties & {
        [k: string]: React.CSSProperties & {
          [k: string]: React.CSSProperties & {
            [k: string]: React.CSSProperties & {
              [k: string]: React.CSSProperties & {
                [k: string]: React.CSSProperties & {
                  [k: string]: React.CSSProperties & {
                    [k: string]: any
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

that way you get strong typing for standard properties up to 10 levels deep, further than that anything goes

@pelotom
Copy link
Member

pelotom commented Apr 12, 2018

Where exactly in Material UI are you proposing a change, or is this an issue with @types/jss? Is it a duplicate of DefinitelyTyped/DefinitelyTyped#24862?

@rosskevin
Copy link
Member

rosskevin commented Apr 12, 2018

As noted here: https://github.com/frenic/csstype#what-should-i-to-do-when-i-get-type-errors

I am now using the following type augmentation css.d.ts - though I'm not sure what the best path is going forward:

import * as CSS from 'csstype'

declare module 'csstype' {
  interface Properties {
    // Add a CSS Custom Property
    '&:hover'?: Properties<string | number>

    // ...or allow any other property
    // [index: string]: any
  }
}

For one-offs where I use jss syntax, I'm casting as any (mentioned by @pelotom in DefinitelyTyped/DefinitelyTyped#24911):

const styles = withStyles<FooClassKey>(theme => ({
  listItem: {
    ['&:hover $listItemIcon' as any]: {
      visibility: 'inherit',
    },
  },
}))

@pelotom
Copy link
Member

pelotom commented Apr 12, 2018

Module augmentation/casting definitely shouldn't be required for this. This is a bug in either @types/jss or material-ui's typings. Looking into it.

@pelotom
Copy link
Member

pelotom commented Apr 12, 2018

I've opened a PR: #11007.

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

4 participants