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

flexDirection is not assignable to CSSProperties #1344

Open
just-Bri opened this issue May 10, 2020 · 13 comments
Open

flexDirection is not assignable to CSSProperties #1344

just-Bri opened this issue May 10, 2020 · 13 comments

Comments

@just-Bri
Copy link

just-Bri commented May 10, 2020

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.

@holylander
Copy link

Thanks @reifnotreef ! your workaround worked for me 👍

@abustamam
Copy link

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: frenic/csstype#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.

@gituser3000
Copy link

whiteSpace: 'nowrap'
same error

@just-Bri
Copy link
Author

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.

@dannyfoncke
Copy link

as React.CSSProperties works for me

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

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


@just-Bri
Copy link
Author

just-Bri commented Nov 26, 2020

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

@dannyfoncke
Copy link

@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)

@stanislavn73
Copy link

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

@ialsowalkdogs
Copy link

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;
  ...
  }
}

@olejorgenb
Copy link

olejorgenb commented Oct 23, 2021

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.

@Faruk-Suljagic
Copy link

Screenshot 2022-01-31 at 15 02 18

This fixed my problem

hakjeri pushed a commit to ClimateCompatibleGrowth/teaching-kit-frontend that referenced this issue Feb 2, 2023
hakjeri added a commit to ClimateCompatibleGrowth/teaching-kit-frontend that referenced this issue Feb 2, 2023
* Add mobile view to core pages, update design

* Updated Dropdown and Tab styling for mobile view

* Weird css issue, see fix cssinjs/jss#1344

---------

Co-authored-by: Håkan Eriksson <hakan.eriksson2@sj.se>
@enBonnet
Copy link

Same problem

ajtran303 added a commit to ajtran303/weather_app_react_ts that referenced this issue Jun 17, 2023
There is an issue with the `flexDirection` property
I am consistently getting an error about it and the only fix I found is
cssinjs/jss#1344 (comment)
which states to add `as React.CSSProperties` when I declare a style
@juburr
Copy link

juburr commented Jul 12, 2023

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}>

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