Prasaad Patil
Prasaad Patil

Reputation: 497

How to import react-cookie in typescript

I am creating an app in react using the react template provided by visual studio 2017. I want to make use of react-cookie in my application. I have installed this lib using command

npm install react-cookie

But when i try to import it in my component it gives me the following error on compile time:

import { Cookies } from 'react-cookie';

(TS) Could not find a declaration file for module 'react-cookie'. 'D:/somepath/node_modules/react-cookie/lib/index.js' implicitly has an 'any' type. Try npm install @types/react-cookie if it exists or add a new declaration (d.ts) file containing declare module 'react-cookie'

I tried installing it using

npm install @types/react-cookie

But the console shows me a message that

@types/react-cookie is not in the npm registry. You should bug the author to publish it (or use the name yourself

I am very new to react so maybe i am making a silly mistake. Can somebody help me out as i need to use this library in my project.

Upvotes: 3

Views: 7221

Answers (1)

Ricardo Saracino
Ricardo Saracino

Reputation: 1420

I found types definitions

{
  "dependencies": {
    "universal-cookie": "^2.1.2",
    "universal-cookie-express": "^2.1.2",
    "react-cookie": "^2.1.2"
  },
  "devDependencies": {
    "@types/universal-cookie": "git+ssh://[email protected]:S64/types-universal-cookie.git#v2.1.2-2",
    "@types/universal-cookie-express": "git+ssh://[email protected]:S64/types-universal-cookie-express.git#v2.1.2-2",
    "@types/react-cookie": "git+ssh://[email protected]:S64/types-react-cookie.git#v2.1.2-2"
  }
}

https://github.com/S64/types-react-cookie

open up git bash from start menu (WINDOWS)

ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts
ssh-keygen -t rsa -C "user.email"

Add the id_rsa.pub key to SSH keys list on your GitHub profile.

if you are really interested look at this commit (passed tslint) https://github.com/ricardosaracino/heroes-react-ts/commit/19a9e0f55940c9ac7007e093d08eae9d17e7d7ba

Upvotes: 1

Related Questions