-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Emotion v10 unusable with pure TypeScript #1046
Comments
@krzysztofzuraw Do you have jsx pragma? It's hard to say without reproducing example. |
@TrySound sure - I will prepare codesandbox 😄 |
@TrySound Codesandbox example: https://codesandbox.io/s/mm0pymo5v8 |
Working example: https://codesandbox.io/s/2j01mr3m1y . I've added Type checking css prop doesnt quite work on this sandbox. Probably @Ailrun will know what to do about it. |
FYI having |
With babel this all can be automated - no need for any manual wiring up. Im just not that experienced with TS to know how to achieve the same with it (I suspect there is one though). |
I see, that means no jsx pragma with the plugin? I haven't found this information in the docs, sorry |
@Andarist |
So it should just work when importing from @emotion/core, right?
…On Fri, 7 Dec 2018 at 17:18, Junyoung Clare Jang ***@***.***> wrote:
@Andarist <https://github.com/Andarist>
TS does not support typechecking based on JSX pragma (we cannot change
typing when users change JSX pragma), so current TS typing just have css
props when users import @emotion/core.
And this issue actually is not about TS, but about the way to use
***@***.***, since @krzysztofzuraw <https://github.com/krzysztofzuraw>
missed JSX pragma, and used className instead of css.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1046 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AJWMkhpNIGGNpcWuEHDmltQJ3QnfI9QEks5u2pTpgaJpZM4Y3prj>
.
|
Type check will work with imported |
Whilst not pretty, a temporary solution is to state /** @jsx jsx */ jsx; For example /** @jsx jsx */ jsx;
import * as React from "react";
import { jsx, css } from "@emotion/core";
const yolo = css({
color: "lightcoral"
}); and then use like so <p css={yolo}>swaggins</p> |
I am struggling to use the css prop and jsx pragma with typescript (using CRA) and I get
@joe-bell this fixes it for me, but aren't you're still having to declare the jsx pragma in each file? |
|
@bitttttten yes, but only in the files where you need to enable it! Not every file. @Ailrun Unfortunately with 2, this doesn't seem to work for unless you call |
Did you mean webpack treeshakes |
@Ailrun sure, in this instance I'm compiling using If I use: /** @jsx jsx */;
import { css, jsx } from "@emotion/core"; No errors appear in the console during compile time but when I consume the outputted However, if I add Environment:
I'd love to be able to offer some support to investigate this issue further but I'm fairly new to TypeScript |
Could you give me the result (the output of the compilation of lib) for a single component with the problem? It seems a TS issue or a Webpack issue to me... |
Using:
Auto saves into
(jsx is removed because auto save cleans up unused variables) It gives, in the browser:
Full dump here: https://pastebin.com/1EBkHRLN tsconfig is:
and eslintrc.json:
|
FWIW the emotion eslint plugins will automatically add pragma and import for you when using the ESLint Check out the others here: https://github.com/emotion-js/emotion/tree/master/packages/eslint-plugin-emotion I'm not sure how this will interact with typescript but it removes the effort of using the pragma import. I just start typing and on save its added for me and I don't think about it again. @bitttttten thanks for the example. I'm going to ask around. |
@bitttttten Which editor do you use? Does you editor also remove |
This is the relevant ESLint rule: https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-uses-react.md And its docs say that it should work with the pragma comment:
|
Maybe the plugins are applied in a wrong order, e.g. the https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-uses-react.md is applied before the pragma comment is added to the source code by https://github.com/emotion-js/emotion/blob/master/packages/eslint-plugin-emotion/docs/rules/jsx-import.md |
TypeScript will not auto-remove unused variables or imports unless you tell it to (it's not part of its downcompile). @joe-bell can you share your |
@Ailrun apologies for the delay, I'm going to need to try to re-create this issue without using my client's code. Will try to give you some more details on this soon |
VSCode
No!
Gets turned into:
That's why I came into this thread.. hehe. I even tried installing eslint-plugin-emotion although it seems to have no effect. I made a repo for this issue, which is just a CRA app. The file I have been talking about is here. |
I'm guessing that the people who are having this issue where adding I'll open an issue on the babel repo. EDIT: There already is an issue opened, and the fix is to set the |
So it's not possible to fix this when using CRA until the latest fix in babel has been released. |
While we are here it would make sense to verify if the same issue happens with Flow so that we can document both. |
@Andarist What does that "custom jsx pragma" look like? Do you just mean |
WTH 😤I've spent hours and this nasty [object Object] thing is not going away anyhow in my typescript react project. |
I know this isn't helpful, but I ended up switching back to Styled Components :( |
@paramsinghvc I'm using Parcel with the following configuration and it works as expected.
|
@karol-majewski Thanks it worked. But I tried using it with css prop babel preset as
It doesn't work without me having to specify |
I have an example repo that's using Babel Typescript, Parcel, Emotion 10 and the CSS prop (no pragma statements required) successfully here: There is a summary of the workarounds with references in the readme. |
@paramsinghvc You shouldn't combine |
try this : LeetCode-OpenSource/emotion-ts-plugin#26 |
For anyone still wanting to use Typescript + Emotion 10 without having to import
Note: Webpack loaders are applied from right to left, that's why babel-loader is listed first
The problem is that typescript only sees the types defined in
I elected to use the reference because when you set a That's all, hope it helps someone. BTW, I think with this approach, you can continue to use the Fragment shorthand <>/</> since Babel is transpiling the jsx |
Do whatever works for you - but this can hardly be called a minor change to existing setups. Using jsx pragma explicitly means that you don't have to do anything special to satisfy all the tooling that wants to understand JSX etc except adding a custom pragma because support for it is backed into nearly all the tooling. 🤷♂ |
@Andarist Correct me if I'm wrong, but one of the main use cases for the babel preset or the babel plugin is to avoid having to add the jsx import and the pragma at the top of each file where you want to use the css prop right? My comment simply shows a way that you can use the plugin with Typescript. That's all. |
As to As to |
@Andarist Yeah, my bad. Just the preset. Which in my opinion is great and listed as the first option in the docs. It took me a while to figure why it wasn't working with Typescript, but like you said, it's the setup that works for me and may not be the best for everyone. I'm in the process of convincing my team to move from styled-components to emotion and I wanted it to be as simple as possible for them. My apologies for replying to a closed ticket, it just happens to be the what popped up when I googled and I wanted to provide some info for others that may want to do something similar. Thanks for all the work on Emotion. It rocks!!! |
No problem - I'm just always rather surprised how strongly people react to the whole custom pragma thing. Your comment can be certainly helpful for others trying to do the same 👍 I just don't understand why people want to go through such hoops 😅 IMHO the ROI is just low for this one, but that might be just me. |
any ideas about getting pretty class names (aka |
@jacek213 you can try this https://github.com/LeetCode-OpenSource/emotion-ts-plugin |
@jacek213 are you using webpack or just transpiling with |
Any updates about this, so we don't need to use |
This is for now how i have it working with: Create React App (CRA) + TypeScript + Craco Using This is how it should look when using with /** @jsx jsx */
import React from 'react'
import { css, jsx } from '@emotion/core'
const myTest = css`
background-color: red;
width: 400px;
height: 400px;
font-size: 10px;
`
function App(): JSX.Element {
return (
<React.Fragment>
<div css={myTest}>Testing Emotion</div>
</React.Fragment>
)
}
export default App And without /** @jsx jsx */
// import React from 'react'
import { css, jsx } from '@emotion/core'
const myTest = css`
background-color: red;
width: 400px;
height: 400px;
font-size: 10px;
`
function App(): JSX.Element {
return (
// <React.Fragment>
<div css={myTest}>Testing Emotion</div>
// </React.Fragment>
)
}
export default App {
"name": "my-app",
"version": "0.1.0",
"private": true,
"homepage": "./",
"dependencies": {
"@emotion/babel-preset-css-prop": "^10.0.27",
"@emotion/core": "^10.0.28",
"@types/react": "^16.9.34",
"@types/react-dom": "^16.9.7",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1",
"typescript": "~3.8.3"
},
"devDependencies": {
"@craco/craco": "^5.6.4",
"@typescript-eslint/eslint-plugin": "^2.31.0",
"@typescript-eslint/parser": "2.x",
"eslint": "7.x",
"eslint-config-prettier": "^6.11.0",
"eslint-config-react-app": "^5.2.1",
"eslint-plugin-flowtype": "4.x",
"eslint-plugin-import": "2.x",
"eslint-plugin-jsx-a11y": "6.x",
"eslint-plugin-prettier": "^3.1.3",
"eslint-plugin-react": "7.x",
"eslint-plugin-react-hooks": "4.x",
"prettier": "^2.0.5"
},
"scripts": {
"dev": "craco start --config config/webpack.dev.js",
"build": "craco build --config config/webpack.prod.js",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
} |
@MariuzM Hi, you can try this plugin: https://github.com/LeetCode-OpenSource/emotion-ts-plugin |
So we have two direction to solve this problem:
For me emotion-ts-plugin was 3x times faster, but i spend much time to found how to declare jsx auto injected to files. import {createElement} from "@types/react";
declare global {
var jsx: typeof createElement = createElement;
} through tsconfig.json option "types": ["./typings/index"], /* Type declaration files to be included in compilation. */ |
emotion
version: 10.0.0react
version: 16.6.3typescript
version: 3.1.6Relevant code.
What you did:
I've tried to use it as:
Or:
What happened:
I've got this in html:
Or:
I'm using only TypeScript without babel.
Problem description:
How can I use
css
orclassName
with emotion v10 and TypeScript?The text was updated successfully, but these errors were encountered: