jss icon indicating copy to clipboard operation
jss copied to clipboard

flexDirection is not assignable to CSSProperties

Open just-Bri opened this issue 5 years ago • 13 comments

flexDirection: string; is not assignable to type 'CSSProperties'

React using TypeScript.

(JSX attribute) HTMLAttributes<HTMLDivElement>.style?: React.CSSProperties
Type '{ backgroundColor: string; display: string; flex: string; flexDirection: string; }' is not assignable to type 'CSSProperties'.
  Types of property 'flexDirection' are incompatible.
    Type 'string' is not assignable to type 'FlexDirectionProperty'.ts(2322)
index.d.ts(1763, 9): The expected type comes from property 'style' which is declared here on type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>'

Get this error when trying to assign a flexDirection in styles:

const styles = {
  root: {
    backgroundColor: 'red',
    display: 'flex',
    flex: 'row',
    flexDirection: 'row',
  },
};

This really confuses me:

Types of property 'flexDirection' are incompatible.
            Type 'string' is not assignable to type '"row" | "-moz-initial" | "inherit" | "initial" | "revert" | "unset" | "column" | "column-reverse" | "row-reverse" | PropsFunc<{}, FlexDirectionProperty>'.ts(2345)

It doesn't know that the string 'row' is === "row"

single quotes are being put in place via eslint/prettier, even when I try using double quotes it doesn't change it. I have also tried nested flex properties in an object:

flex : {
  direction: 'row',
}

and

flex: {
  flexDirection: 'row',
},

But neither of those work.

EDIT:

I found a workaround:

flexDirection: 'row' as 'row',

This allows it to work as expected but is super clunky.

just-Bri avatar May 10 '20 16:05 just-Bri

Thanks @reifnotreef ! your workaround worked for me 👍

holylander avatar Aug 04 '20 11:08 holylander

This appears to be an issue with the CSSType library, I think. Notice here that JustifyContent has the (string & {}) at the end: https://github.com/frenic/csstype/blob/master/index.d.ts#L18504

But it's missing from FlexDirection: https://github.com/frenic/csstype/blob/master/index.d.ts#L18324

There's been some discussion about it: https://github.com/frenic/csstype/issues/84, but nothing directly relating to this particular issue.

Also, you can use const assertions as of TS 3.4: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html, so it becomes 'row' as const.

abustamam avatar Sep 19 '20 17:09 abustamam

whiteSpace: 'nowrap' same error

gituser3000 avatar Nov 20 '20 17:11 gituser3000

Wanted to drop a note on here that this does not happen using react-jss and createUseStyles.
Obviously something is bit wonky with the types but I'm not sure what the fix would be.

just-Bri avatar Nov 20 '20 18:11 just-Bri

as React.CSSProperties works for me

let labelPosition  = {
        display: "flex",
        flexDirection: "row",
    }

<div style={labelPosition as React.CSSProperties}>


dannyfoncke avatar Nov 26 '20 16:11 dannyfoncke

as React.CSSProperties works for me

let labelPosition  = {
        display: "flex",
        flexDirection: "row",
    }

<div style={labelPosition as React.CSSProperties}>

You are just using a style tag and a JS object, this has no direct interaction with JSS.
For reference you can do this in a jsx file without having jss installed at all: https://repl.it/join/ddppwbhd-reifnotreef

just-Bri avatar Nov 26 '20 16:11 just-Bri

@reifnotreef Sorry, I reacted to the question not realizing it was specific to JSS I had the same problem (not using JSS) in typescript and didn't look any further If you prefer I can delete my first (and this) post as it is not relevant (and not to clutter this space)

dannyfoncke avatar Nov 27 '20 07:11 dannyfoncke

Seems this problem appearing when using withStyles. When using makeStyle problem is gone

stanislavn73 avatar Jun 03 '21 07:06 stanislavn73

This is a TS issue and it happens because in the absence of typing, your flexDirection is cast as string and it cannot be coerced into the string literal matching FlexDirectionProperty.

Does it work if you provide an explicit type for your styles, like:

type Styles = { 
  root: {
    flexDirection: FlexDirectionProperty;
  ...
  }
}

ialsowalkdogs avatar Aug 17 '21 16:08 ialsowalkdogs

Seems this problem appearing when using withStyles. When using makeStyle problem is gone

Hm.. but I get the problem when using the createStyles as input to makeStyles.

A bit of a side track, but I was under the impression that one had to use the createStyles wrapper, but that doesn't seem to be the case. Webstorm also provides better support for object literal passed directly to makeStyles

PS: I get the same problem for the boxSizing, alignItems and whiteSpace properties.

olejorgenb avatar Oct 23 '21 18:10 olejorgenb

Screenshot 2022-01-31 at 15 02 18 This fixed my problem

Faruk-Suljagic avatar Jan 31 '22 14:01 Faruk-Suljagic

Same problem

enBonnet avatar Apr 26 '23 14:04 enBonnet

You can also declare the object type immediately versus trying to cast it later on. This is a variation of one of the top answers from above.

let labelPosition: React.CSSProperties  = {
    display: "flex",
    flexDirection: "row",
}

<div style={labelPosition}>

juburr avatar Jul 12 '23 01:07 juburr