error TS2322: Property 'css' does not exist on type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>'
Asked Answered
R

7

6

I've recently decided to move my codebase from JS to TS to add Type checking. I also use the styled-components library.

However, i'm running into the following issue:

error TS2322: Type '{ children: string; css: string; }' is not assignable to type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>'.
  Property 'css' does not exist on type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>'.

Here is a basic code example:

import React from 'react'
const MyComponent: React.FC = (props) => {
    return (
        <div css={`
            padding: 44px;
        `}>
            My content
        </div>
    )
}

Please note that my tsconfig.json compilerOptions look like this:

"compilerOptions": {
        "composite": true,
        "declaration": true,
        "outDir": "./dist", // https://www.typescriptlang.org/tsconfig#outFile
        "rootDir": "./src",
        "jsx":"react",
        "types": ["@types/styled-components"],
        "module": "commonjs",
        "target": "es5",
        "sourceMap": true,
        
        "declarationMap": true,
        "noEmitOnError": true,
        "skipLibCheck": true,
        "esModuleInterop": true,
        "noUnusedLocals":true,
        "noUnusedParameters":true,
        "allowJs":true,
        "noEmit": false
    }

Can anyone point me in the right direction to solve the type of the css attribute ?

Ruhr answered 26/11, 2021 at 16:33 Comment(1)
The attribute you are looking for is style instead cssFormality
V
5

Create styledComponentsOverride.d.ts with following code inside:

import { CSSProp } from 'styled-components'

interface MyTheme {}

declare module 'react' {
    interface Attributes {
       css?: CSSProp<MyTheme>
    }
}

This will extend react element attributes types with optional CSS prop

more info: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/31245

Velamen answered 26/11, 2021 at 16:46 Comment(0)
M
3

Why not just add this line in the 'style.d.ts' file?

import {} from 'styled-components/cssprop'
Micah answered 16/2, 2022 at 17:16 Comment(1)
I added this into my global.d.ts file, and this resolved my problem! Thanks!Flora
S
3

If anyone is experiencing this issue while using emotion, see the equivalent type declaration module:

import type { SerializedStyles } from "@emotion/serialize";
import type { DOMAttributes } from "react";

declare module "react" {
    interface HTMLAttributes<T> extends DOMAttributes<T> {
        css?: SerializedStyles;
    }
}

Schematize answered 21/4, 2023 at 16:8 Comment(0)
R
1

Thanks @jkaczmarkiewicz ! This works great. Updated it to :

import { CSSProp } from "styled-components";

interface DefaultTheme {}

declare module "react" {
   interface HTMLAttributes<T> extends DOMAttributes<T> {
     css?: CSSProp<DefaultTheme>;
   }
 }

In addition to the link you provided, this issue might help others too.

Ruhr answered 26/11, 2021 at 17:5 Comment(0)
W
0

I did this in a similar, but slightly different way. I added a styled.d.ts file in the src/theme folder. You can also just put it straight in the src file if you're not using a theme.

Then I added the following code in this file:

import { CSSObject } from 'styled-components';
import { CSSProp } from 'styled-components';
declare module 'react' {
  interface Attributes {
    css?: CSSProp | CSSObject;
  }
}

Sources

Whidah answered 8/6, 2022 at 13:16 Comment(0)
E
0

In emotion you can create the following cssTypes.d.ts

import { Interpolation } from '@emotion/react';

interface DefaultTheme {}

declare module 'react' {
  interface HTMLAttributes<T> extends DOMAttributes<T> {
    css?: Interpolation<DefaultTheme>;
  }
}
Evanescent answered 1/7, 2023 at 5:27 Comment(0)
R
0

Getting the same error when trying to compile my tsx files. I found an unexpected solution on github. If using TypeScript, add this to tsconfig.json under compilerOptions:

 "jsxImportSource": "@emotion/react",

Since I'm using MUI v5, I already had a reference to @emotion/react in my package.json file.

And if using eslint, also add this to .eslintrc.js to avoid syntax errors in the IDE:

 "rules": {
   "react/no-unknown-property": ['error', { ignore: ['css'] }]
 }

With those changes, everything started working and I was able to use the css prop on a div in .tsx files.

Ramillies answered 14/4, 2024 at 14:47 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.