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

Typescript & withStyles error when has 'position' #8928

Closed
1 task done
voronindev opened this issue Oct 31, 2017 · 6 comments
Closed
1 task done

Typescript & withStyles error when has 'position' #8928

voronindev opened this issue Oct 31, 2017 · 6 comments

Comments

@voronindev
Copy link

voronindev commented Oct 31, 2017

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
  }
}
  • 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
@pelotom
Copy link
Member

pelotom commented Nov 1, 2017

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
Copy link
Member

pelotom commented Nov 1, 2017

If #8819 is merged, you could also do

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

@adriantoine
Copy link

adriantoine commented Nov 9, 2017

@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',
    // ...
  },
}));

@pelotom
Copy link
Member

pelotom commented Nov 9, 2017

@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',
      // ...
    },
  }));

@leotm
Copy link

leotm commented Nov 22, 2021

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

@padraicohora
Copy link

padraicohora commented May 20, 2022

@pelotom

Thanks for that, I would never have know this

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

6 participants