Error

Error | 'css' does not exist on type 'DetailedHTMLProps...' with new JSX

2022. 8. 22. 23:46
목차
  1. Emotion 도입 이유
  2. 해결방법

새로 시작한 프로젝트에서 styled-component 남용을 방지하기 위해 emotion을 도입하여 사용하기로 했다.

Emotion 도입 이유

간단한 스타일이 필요한 요소는 별도로 styled-component로 생성하지 않고 시맨틱 태그와 함께 inline style을 적용하여 구현한다면 코드도 더욱 간결해지고 시멘틱 태그의 적절한 사용으로 더나은 SEO에 기여할 수 있을 것으로 생각되었기 때문이다.

에러발생 코드

...
import { css } from '@emotion/react';

const Layout = ({ children }: LayoutProps) => {
  return (
    <div
      css={css`
        width: 90%;
        margin: 0 auto;
      `}
    >
      <Navbar />
      {children}
      <Floating />
    </div>
  );
};

위와 같이 Layout 컴포넌트에 emotion을 사용하여 inline-style을 적용하려고 했는데, 에러가 발생했다. 

Error Message
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>'.

해결방법

처음엔 에러메시지를 보고 type 설정을 제대로 해주지 않아서 발생한 에러 혹은 airbnb-style-guide 로 인한 에러일 것으로 판단하고 구글링을 했다.

airbnb-style-guide 관련해서는 별다른 해결방법을 찾을 수 없었고, Layout 컴포넌트에서 @emotion/react 를 import 하고 tsconfig.json 파일 compileOptions에 "types": ["@emotion/core"] 속성을 추가해서 해결했다는 글을 발견 했으나, 이 방법의 경우 또 다른 에러를 출력했다. 

 

다른 자료를 찾아보니 아래와 같이 tsconfig.json 파일 CompileOptions 의 jsx의 값을 reac-jsx로 수정하고, "jsxImportSource": "@emotion/react" 속성을 추가하는 것으로 간단하게 해결할 수 있었다. 

자료를 조금 더 찾아보았지만 정확히 어떤 로직으로 문제가 해결되는지는 이해하지 못했으나 react에서 jsx를 컴파일해서 사용하고, emotion/react를 사용하여 해당 jsx 를 import 하는 것을 config에 지정을 해줘야 한다고 어림 짐작이 된다.

 

타입스크립트 핸드북을 한번 훑어보긴 했는데, 이런 부분을 전혀 캐치하지 못했다. 프로젝트를 진행하면서 이런 이해가 안되는 부분을 기억하고 추후 다시 핸드북을 학습하면서 중점적으로 학습해야겠다. 

참고

https://www.typescriptlang.org/tsconfig#jsxImportSource

 

TSConfig Reference - Docs on every TSConfig option

From allowJs to useDefineForClassFields the TSConfig reference includes information about all of the active compiler flags setting up a TypeScript project.

www.typescriptlang.org

https://github.com/emotion-js/emotion/issues/2111#issuecomment-732763318

 

TS2322: 'css' does not exist on type 'DetailedHTMLProps...' with new JSX runtime w/ NextJs · Issue #2111 · emotion-js/emotion

Current behavior: I have used NextJs v10.0.1 and emotionJs v11.0.0. I installed the @emotion/babel-plugin and followed the new jsx runtime setup with NextJs in this page. { "presets": [ [...

github.com

Environment Information
  • react: 18.2.0
  • next: 12.2.5
  • typescript: 4.7.4
  • @emtion/react: 11.10.0

'Error' 카테고리의 다른 글

Error | 중첩된 객체(객체 안의 객체) 형태의 상태 onChange 함수 구현하기  (0) 2023.08.31
msw 에러 `serializeResponse.ts:14 Uncaught (in promise)14 TypeError: Failed to execute 'text' on 'Response'`  (0) 2023.08.09
msw로 api 모킹하려는데!! '[Symbol.iterator]()' 메서드 에러  (0) 2023.07.07
typescript + jest 테스트 코드 에러 (Jest encountered an unexpected token)  (0) 2023.06.26
Firebase | 실시간 데이터 베이스(Realtime Database) COR 에러와 401 Unauthorized 에러  (0) 2023.06.05
  1. Emotion 도입 이유
  2. 해결방법
'Error' 카테고리의 다른 글
  • msw 에러 `serializeResponse.ts:14 Uncaught (in promise)14 TypeError: Failed to execute 'text' on 'Response'`
  • msw로 api 모킹하려는데!! '[Symbol.iterator]()' 메서드 에러
  • typescript + jest 테스트 코드 에러 (Jest encountered an unexpected token)
  • Firebase | 실시간 데이터 베이스(Realtime Database) COR 에러와 401 Unauthorized 에러
3jun
3jun
3jun
3jun
3jun
전체
오늘
어제
  • 분류 전체보기 (94)
    • 프로젝트 (5)
    • Web (9)
    • JavaScript (19)
    • React (12)
    • 알고리즘 (31)
    • Git (3)
    • AWS (3)
    • TypeScript (3)
    • CS (2)
    • Error (6)
    • 회고 (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • state
  • 백준 온라인저지
  • msw
  • 프로그래머스 코딩테스트
  • msw 에러
  • 백준 js
  • react
  • airbnb style guide
  • this.props.history.push
  • 프로그래머스 코딩테스트 js
  • 백준 코테
  • 프로그래머스 js
  • 백준 알고리즘
  • 프로그래머스 코테
  • outer environment
  • 백준js
  • Promise
  • 유효성 로직
  • 자바스크립트
  • JavaScript

최근 댓글

최근 글

hELLO · Designed By 정상우.
3jun
Error | 'css' does not exist on type 'DetailedHTMLProps...' with new JSX
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.