TypeScript Error: Could not find a declaration file for module ‘redux-persist/lib/storage’.

redux-persist의 storage 모듈 타입 선언 오류를 삼중 슬래시 지시문으로 해결한 기록.

React, TypeScript

import persistReducer from "redux-persist/lib/persistReducer";
import storage from "redux-persist/lib/storage";

타입스크립트에서 redux-persist를 사용할 때, storage와 persistReducer 등을 import하는 경우, 간혹 생기는 에러가 있다.

Could not find a declaration file for module ‘redux-persist/lib/storage’.

“모듈 ‘redux-persist/lib/storage’에 대한 선언 파일을 찾을 수 없습니다.” 라는 이 에러는 말 그대로 타입 정의가 없다는 얘기인데 타입스크립트에서는 타입 체커가 엄격하게 타입을 검사하기 때문에 발생하는 에러였다.

그러면서 해당 라이브러리의 타입 정의를 설치하거나, 정의 파일을 만들 것을 권유하는데, npm install @types/redux-persist -D를 입력해서 타입 정의 라이브러리를 설치해봐도 소용이 없었고, npmjs.com에서도 타입스크립트 정의 파일이 포함되어 있다고 적혀있었지만 에러를 해결할 수 없었다.

해당 에러의 솔루션을 Stack overflow에서 찾을 수 있었고 생각보다 간단했다.

/// <reference types="redux-persist" />

CRA(create-react-app)를 통해 프로젝트를 진행하고 있는 경우, src 폴더 내부의 react-app-env.d.ts 파일에 — 없다면 생성해서— 위와 같이 선언을 해주면 해결되었다.

TypeScript 한글 문서

How to add redux-persist to typescript project?


원문: Medium