이 영역을 누르면 첫 페이지로 이동
Nuhends 의 Tech Life 블로그의 첫 페이지로 이동

Nuhends 의 Tech Life

페이지 맨 위로 올라가기

Nuhends 의 Tech Life

IT / Tech / 재테크 관련 뉴스를 최대한 알기 쉽게 전달하는 Tech 블로그 입니다.

[Emotion] CSS 속성 사용시 타입 에러 해결 방법(with Next.js)

  • 2022.09.07 21:33
  • 프로그래밍/NextJS
반응형

[Emotion] CSS 속성 에러 해결 방법(with Next.js)

 

styled component emotion css property type error

 

 

이슈 발생

아래와 같이 @emotion/react 모듈의 css 함수를 사용하면 type 에러가 발생한다.

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

// ...

<div
	css={css`
          padding: 32px;
          background-color: hotpink;
          font-size: 24px;
          border-radius: 4px;
        `}
>
        Blog
</div>

emotion css property error ts2322

더보기

에러내용 (ts.2322)

Type '{ children: string; css: SerializedStyles; }' is not assignable to type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>'. Property 'css' does not exist on type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>'.ts(2322)

 

원인

@types/react/index.d.ts의 HTMLAttributes interface에 css 속성이 없는데, 추가하려고 하니 타입 에러가 발생

(해당 코드: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L1843-L1909)

 

 

해결 방법

1. tsconfig.json > compileOptions 에 types 추가

// tsconfig.json
{
    "compilerOptions": {
    	"types": ["@emotion/react/types/css-prop"]
    }
}

 

2. styles.d.ts 추가

styled.d.ts 파일을 추가하고, 아래 코드를 추가

/// <reference types="@emotion/react/types/css-prop" />

 

emotion 공식 홈페이지 설명

 

어떤 방법을 선택해야하는가?

필자는 1번 방법을 적용해도 타입에러가 계속 발생해서, emotion 공식 문서에 나와있는 2번 방법을 선택했다. 이유는 아직 왜인지 모르겠다. 시간 날 때 디깅 해봐야겠다.

 

 

반응형

 

반응형
저작자표시 비영리 변경금지 (새창열림)

'프로그래밍 > NextJS' 카테고리의 다른 글

[Next.js] 번역-Rendering Fundamentals  (0) 2023.04.09
[Next.js] 딥링크, Next/Link에서 이슈  (0) 2022.10.31
[Next.js] jest로 unit Test 하기  (0) 2022.10.31
[Emotion] emotion(with next v12) 세팅 방법  (1) 2022.09.07

댓글

이 글 공유하기

  • 구독하기

    구독하기

  • 카카오톡

    카카오톡

  • 라인

    라인

  • 트위터

    트위터

  • Facebook

    Facebook

  • 카카오스토리

    카카오스토리

  • 밴드

    밴드

  • 네이버 블로그

    네이버 블로그

  • Pocket

    Pocket

  • Evernote

    Evernote

다른 글

  • [Next.js] 번역-Rendering Fundamentals

    [Next.js] 번역-Rendering Fundamentals

    2023.04.09
  • [Next.js] 딥링크, Next/Link에서 이슈

    [Next.js] 딥링크, Next/Link에서 이슈

    2022.10.31
  • [Next.js] jest로 unit Test 하기

    [Next.js] jest로 unit Test 하기

    2022.10.31
  • [Emotion] emotion(with next v12) 세팅 방법

    [Emotion] emotion(with next v12) 세팅 방법

    2022.09.07
다른 글 더 둘러보기

정보

Nuhends 의 Tech Life 블로그의 첫 페이지로 이동

Nuhends 의 Tech Life

  • Nuhends 의 Tech Life의 첫 페이지로 이동
반응형

검색

메뉴

  • 홈
  • 웹 개발
  • 경제 데이터
  • 경제 공부
  • 방명록

카테고리

  • 분류 전체보기 (127)
    • 프로그래밍 (69)
      • React (3)
      • HTML&CSS 사전 (13)
      • JAVASCRIPT 사전 (11)
      • Algorithm (23)
      • 이슈 정리 (2)
      • 개발 환경 (4)
      • NodeJS (1)
      • Typescript (4)
      • NextJS (5)
      • React-Query (2)
      • ai (1)
    • 경제 데이터 (22)
      • 주식 순위 (20)
      • 경제지표 (2)
    • 경제 공부 (25)
      • 경제 신문 읽기 (3)
      • 세금 재테크 (7)
      • 인사이트 (4)
      • 경제용어정리 (9)
      • 정부 지원 제도 관련 (2)
    • 팁 모음 (11)
      • 인터넷 (5)
      • 생활 (3)
      • SNS 맛집 (3)

최근 글

인기 글

댓글

공지사항

아카이브

태그

  • 자바스크립트
  • 프로그래머스
  • 코테 풀이
  • softeer
  • 코딩테스트
  • 알고리즘
  • 코테
  • javascript

나의 외부 링크

정보

nuhends의 Nuhends 의 Tech Life

Nuhends 의 Tech Life

nuhends

블로그 구독하기

  • 구독하기
  • RSS 피드

방문자

  • 전체 방문자
  • 오늘
  • 어제

티스토리

  • 티스토리 홈
  • 이 블로그 관리하기
  • 글쓰기
Powered by Tistory / Kakao. © nuhends. Designed by Fraccino.

티스토리툴바