emotion: TS2322: Property 'css' does not exist...
emotion: version: 10.0.7emotion-theming: 10.0.7@emotion/core: version: 10.0.7@emotion/styled: version: 10.0.7@emotion/cache: version: 10.0.reactversion: 16.7.0typescriptversion: 3.2.4
Relevant code:
tsconfig.json:
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"allowUnreachableCode": false,
"checkJs": false,
"downlevelIteration": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"jsx": "preserve",
"lib": ["webworker", "esnext", "dom"],
"module": "esnext",
"moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
"noImplicitAny": false,
"noImplicitReturns": true,
"noImplicitThis": false,
"noUnusedLocals": false,
"outDir": "./build/",
"pretty": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": false,
"strictNullChecks": false,
"suppressImplicitAnyIndexErrors": true,
"target": "esnext",
"types": ["@emotion/core", "jest", "node", "webpack-env"]
},
"include": ["./src/**/*", "./monaco.d.ts"],
"exclude": [
"node_modules",
"build",
"scripts",
"acceptance-tests",
"webpack",
"jest",
"gamma.config.js"
]
}
Example TS error:
TS2322: Type '{ children: Element; css: SerializedStyles; }' is not assignable to type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>'.
Property 'css' does not exist on type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>'.
pragma
/** @jsx jsx */
import { jsx, css } from '@emotion/core'
import * as React from 'react'
...
Related Issues:
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 29
- Comments: 21 (2 by maintainers)
For Emotion 11 use the following configuration in your
tsconfig.jsonuntil TypeScript 4.1 comes out:Documentation: https://emotion.sh/docs/emotion-11#css-prop-types
TL;DR: for React 17, Typescript 4.2, Emotion 11.1
tsconfig.jsonThis seems to be the cleanest solution that works for me that does NOT require:
/// <reference types="@emotion/react/types/css-prop" />to a.d.tsfile/** @jsxImportSource @emotion/react */with any importsSee: Emotion TypeScript
cssprop docsAdditional Context:
Prior comments and the Emotion 11 docs linked in prior comments seemed to suggest that upgrading to a version of TypeScript > 4.1 would somehow resolve this issue (and by “resolve” I presumed this would mean removing the need for either the suggested
tsconfig.jsonchanges, or the other change suggested by a few others above on adding a/// <reference types="@emotion/react/types/css-prop" />line in some.d.tsfile in your codebase. Unfortunately this doesn’t seem to be case.I fixed this by changing my tsconfig.json to:
For me, it was adding
/// <reference types="@emotion/react/types/css-prop" />tonext-env.d.tsfile. See https://emotion.sh/docs/emotion-11#css-prop-typesSee https://github.com/UnlyEd/next-right-now/commit/2a3c89dad00cf212929a54b8d4bb2d14f0677af5
thanks that worked for me!
React 17 with Typescript 4.2 and Emotion.js 11.1 setup
Just add the following line to
react-app-env.d.tsas stated in https://emotion.sh/docs/emotion-11/// <reference types="@emotion/react/types/css-prop" />and import with
in tsconfig.json make shure to have
"jsx": "react-jsx"If it helps somehow, you can create custom type declaration like next one
and specify in
typeRoots: ['./@types', './node_modules/@types'], order matters!Are there any updates on this issue? I am still getting this problem. Is a special tsconfig required?
Edit: It work with a basic create-react-app setup, but it stops working, when I am importing an npm package, which includes emotion components. Sadly I don’t have deep enough knowledge of emotion or typescript to make something about that.
Ran into the same issue. If you have already tried @nhooyr 's solution and still get errors. The next step would be a fresh install of dependencies. (npm install or yarn install). If that doesn’t work, try nuking (deleting) the node_modules folder and do a fresh reinstall then reload your IDE / text editor
Or update this
npm install --save @emotion/cssThis is work for me without error.
@SRochas Please always try to share a repro case in a runnable form - either by providing a git repository to clone or a codesandbox. OSS maintainers usually can’t afford the time to set up the repro, even if exact steps are given.
What do you do if you’re already using
typeRootsintsconfig.json, which isn’t compatible withtypes?