简体   繁体   English

如何在打字稿中导入react-cookie

[英]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. 我正在使用visual studio 2017提供的react模板创建一个应用程序。我想在我的应用程序中使用react-cookie I have installed this lib using command 我已经使用命令安装了这个lib

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'. (TS)找不到模块'react-cookie'的声明文件。 'D:/somepath/node_modules/react-cookie/lib/index.js' implicitly has an 'any' type. 'D:/somepath/node_modules/react-cookie/lib/index.js'隐式具有'任意'类型。 Try npm install @types/react-cookie if it exists or add a new declaration (d.ts) file containing declare module 'react-cookie' 尝试npm install @ types / react-cookie(如果存在)或添加包含声明模块'react-cookie'的新声明(d.ts)文件

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. @ types / react-cookie不在npm注册表中。 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. 有人可以帮助我,因为我需要在我的项目中使用这个库。

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://git@github.com:S64/types-universal-cookie.git#v2.1.2-2",
    "@types/universal-cookie-express": "git+ssh://git@github.com:S64/types-universal-cookie-express.git#v2.1.2-2",
    "@types/react-cookie": "git+ssh://git@github.com:S64/types-react-cookie.git#v2.1.2-2"
  }
}

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

open up git bash from start menu (WINDOWS) 从开始菜单(WINDOWS)打开git bash

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. 将id_rsa.pub密钥添加到GitHub配置文件上的SSH密钥列表中。

if you are really interested look at this commit (passed tslint) https://github.com/ricardosaracino/heroes-react-ts/commit/19a9e0f55940c9ac7007e093d08eae9d17e7d7ba 如果你真的有兴趣看看这个提交(通过tslint) https://github.com/ricardosaracino/heroes-react-ts/commit/19a9e0f55940c9ac7007e093d08eae9d17e7d7ba

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM