material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

Typescript & withStyles error when has 'position'

Open voronindev opened this issue 9 years ago • 6 comments

Compilation error, when: export const Desktop = withStyles(styles)<Page>(DesktopComponent)

Where:

const styles = {
  container: {
    position: 'absolute', // <-- if we will remove this line, all will be ok
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    padding: 10
  }
}
  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

Looks like it's a default key, but I have an error.

Current Behavior

Have following compilation error:

error TS2345: Argument of type '{ container: { position: string; top: number; left: number; right: number; bottom: number; paddin...' is not assignable to parameter of type 'Record<"container", Partial<CSSProperties>> | StyleRulesCallback<"container">'.
  Type '{ container: { position: string; top: number; left: number; right: number; bottom: number; paddin...' is not assignable to type 'StyleRulesCallback<"container">'.
    Type '{ container: { position: string; top: number; left: number; right: number; bottom: number; paddin...' provides no match for the signature '(theme: { direction: "ltr" | "rtl"; palette: Palette; typography: Typography; mixins: Mixins; breakpoints: Breakpoints; shadows: ["none", string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string]; transitions: Transitions; spacing: Spacing; zIndex: ZIndex; }): Record<"container", Partial<CSSProperties>>'.

Context

Your Environment

Tech Version
Material-UI 1.0.0-beta.19
React 16.0.0
browser Chrome 61.0.3163.100
etc Typescript template

voronindev avatar Oct 31 '17 19:10 voronindev

This is is due to TypeScript's type widening. It's treating position as a string instead of the literal type you've assigned. You can fix it a number of ways:

const styles = {
  container: {
    // explicitly cast as literal type
    position: 'absolute' as 'absolute',
    // ...
  },
}
// indicate the type at the top level, causing literals to be inferred within the object
const styles: StyleRules<'container'> = {
  container: {
    position: 'absolute',
    // ...
  },
}
// apply withStyles immediately to the styles object, also allowing inference to defeat type widening
const decorate = withStyles({
  container: {
    position: 'absolute',
    // ...
  },
}

Personally I favor the last option.

pelotom avatar Nov 01 '17 03:11 pelotom

If #8819 is merged, you could also do

const styles = {
  container: style({
    position: 'absolute',
    ...
  }),
}

pelotom avatar Nov 01 '17 03:11 pelotom

@pelotom thanks for the help!

The last one doesn't work for me when I use a function:

const decorate = withStyles(({ palette }) => ({
  container: {
    position: 'absolute',
    // ...
  },
}));

adriantoine avatar Nov 09 '17 09:11 adriantoine

@adriantoine in this case TypeScript's type inference breaks down... the solution is to provide a type parameter of the class keys:

const decorate = withStyles<'container'>(({ palette }) => ({
    container: {
      position: 'absolute',
      // ...
    },
  }));

pelotom avatar Nov 09 '17 18:11 pelotom

i ended up

Screenshot 2021-11-22 at 15 48 35

position: 'absolute' as const

Screenshot 2021-11-22 at 15 48 52


equiv to prev example

position: 'absolute' as 'absolute'

but cleaner in latest typescript

leotm avatar Nov 22 '21 15:11 leotm

@pelotom

Thanks for that, I would never have know this

padraicohora avatar May 20 '22 08:05 padraicohora