-
-
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
CSS prop incompatible with typescript #693
Comments
@cdock1029 Is this still relavant with 9.2.2? |
No this seems to be resolved with current version, thanks. |
Close this based on #693 (comment) |
@Ailrun afaict this seems to not work w/ preact-emotion (thanks for typing that btw!) |
@aaronjensen Oh, really? I need to check Preact typing again. |
<!-- Thanks for your interest in the project. I appreciate bugs filed and PRs submitted! Please make sure that you are familiar with and follow the Code of Conduct for this project (found in the CODE_OF_CONDUCT.md file). Also, please make sure you're familiar with and follow the instructions in the contributing guidelines (found in the CONTRIBUTING.md file). If you're new to contributing to open source projects, you might find this free video course helpful: http://kcd.im/pull-request Please fill out the information below to expedite the review and (hopefully) merge of your pull request! --> <!-- What changes are being made? (What feature/bug is being fixed here?) --> **What**: This adds typescript tests for the css prop for both React and Preact. The Preact tests are currently failing, so they'd need to be fixed before this can be merged. See #693 (comment) <!-- Why are these changes necessary? --> **Why**: The tests weren't there before and the css prop only worked with React. <!-- How were these changes implemented? --> **How**: I'm not sure how to answer this question. <!-- Have you done all of these things? --> **Checklist**: <!-- add "N/A" to the end of each line that's irrelevant to your changes --> <!-- to check an item, place an "x" in the box like so: "- [x] Documentation" --> - [ ] Documentation N/A - [x] Tests - [x] Code complete <!-- feel free to add additional comments --> <!-- Please add a `Tag:` prefixed label from the labels so that this PR shows up in the changelog -->
I had to add
for the addition of the |
This definition now lives in the core package — so the entry in emotion/packages/core/types/index.d.ts Lines 84 to 88 in 3403143
|
Huh, I can't seem to get this to work in a fresh codebase. Here's a small reproduction: https://github.com/kentcdodds/ts-and-emotion-not-working Running
You can look at the repo, but here are the details to save you a click: example.tsx import * as React from 'react'
export default () => (
<>
<div style={{marginTop: 10}}>style works</div>
<div css={{marginTop: 10}}>css does not</div>
</>
) tsconfig.json {
"types": ["node", "@emotion/core"],
"compilerOptions": {
"noEmit": true,
"jsx": "react"
},
"include": ["example.tsx"]
} I've got react, Any ideas or things that I'm missing would be appreciated! I'm trying to get gatsby + gatsby-plugin-emotion + gatsby-plugin-typescript to work nicely together :) |
You're augmenting the wrong thing (rather, the compiler's JSX support is weird in a lot of ways and the only sensible thing to augment that works with both intrinsic and non-intrinsic components is this). The augmentation you are looking for is declare module 'react' {
interface Attributes {
css?: InterpolationWithTheme<any>
}
} |
Thanks @Kovensky! So you're saying that this should be I'll double check locally then I can make a PR for that. Thank you! |
Yeah, and you can leave the This is how I did it for |
Hmmm... That doesn't seem to be working for me. I'm trying to make the change to my local |
Do you need to specify the emotion jsx factory to use the This compiles when I run /** @jsx jsx */
import * as React from 'react'
import { jsx } from '@emotion/core'
export default () => (
<React.Fragment>
<div style={{marginTop: 10}}>style works</div>
<div css={{marginTop: 10}}>css does not</div>
</React.Fragment>
) |
@kentcdodds Right, |
Thanks @BarryThePenguin! That does work but:
Is there any way we can make this work while maintaining those two? |
Thanks for the help friends :)
I thought that we could force these typings to be included if we add the |
@kentcdodds Oh, I just realized, your (also, I'd suggest using |
Fantastic, thank you so much @Kovensky! That solved it for me <3 So I'm a bit put off by this solution because of this note in the docs:
I feel like it would be nice to not have to keep this list updated and just auto-include all the |
That would automatically make any I personally avoid it (even giving |
Interesting. Thank you for sharing that @Kovensky! You've been very helpful to me tonight (both here and on twitter! 🙏) |
Thanks for helping out @Kovensky 👍 |
I don't understand. This doesn't work with styled components either. What do you mean by opt in? |
If anyone is having trouble with getting this to work, nuking Not ideal, but it does the job. |
I was trying to figure out this issue in relation to a simple app created with https://github.com/zeit/next.js (awesome package btw!) (note this wasn't failing the build, but just complaining in VSCode) After much fumbling it seemed somehow to a separate install of @types/react under @types/next/node_modules. Since @types/react was already under my main folder's node_modules, I didn't feel adding it was required. But by hovering over a declare module 'react' reference, I saw it was pointing to this underlying node_modules folder. TLDR: By doing
it fixed the problem, seemingly so that @types/next could just point to the version already installed by @types/react in package.json (educated guess), so it didn't add any additional install Couldn't reproduce after this, but hope this helps someone ;) |
The TypeScript workflow could be simplified by providing a |
I'm working with a monorepo built using |
If you have errors with types "type" and "as" you should redefine this types before intert these in styled components
|
For those poor travellers using Emotion 11 the answer is here: #1249 (comment) |
If you are already on TS 4.1 you should follow the first recommendation here: https://emotion.sh/docs/typescript#css-prop It's all in the docs... |
Thanks I tried that and it didn't work, but now I realise it requires a VSCode restart to take effect there. |
I'm pretty sure that I didn't have to restart VScode but maybe you have VScode configured differently than me and some caching has prevented it from reloading the TypeScript server/program. |
React 17 with Typescript 4.2 and Emotion.js 11.1 setupJust add the following line to and import with
in tsconfig.json make shure to have |
I had a similar issue spreading the rest of my props from
|
@neaumusic export interface MyComponentProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'css'> {
someProp: string;
};
export const MyComponent: React.FC<MyComponentProps> = ({
someProp, className
}) => (
<div
css={css`
background-color: "black"
`}
className={className}
/>
); |
emotion
version:9.1.3
react
version:16.4.0
react-emotion
:9.1.3
react-app-rewire-emotion
:4.0.0
babel-plugin-emotion
:9.1.2
@types/react
:16.3.4
@types/react-dom
:16.0.5
Relevant code.
What you did:
try to compile
App.tsx
What happened:
Compilation error:
Problem description:
Typescript doesn't understand
css
prop.The text was updated successfully, but these errors were encountered: