【问题标题】:error TS7016: Could not find a declaration file for module ''. '' implicitly has an 'any' type. TypeScript 2.0错误 TS7016:找不到模块“”的声明文件。 '' 隐含了一个 'any' 类型。打字稿 2.0
【发布时间】:2017-05-19 09:59:27
【问题描述】:

我正在尝试使用模块react-image-gallery。但是,使用 npm 找不到此包的类型。

npm install @types/react-image-gallery 返回 404。

按照this 的回答,我尝试像这样声明ImageGallery:const ImageGallery = require('react-image-gallery');

这不会给出编译器警告,但应用程序在运行时会失败。

我可以通过在tsconfig.json 中设置"noImplicitAny": false 来使一切正常工作,但我不想全局禁用它。

如何将我的导入标记为任何?

我尝试了以下方法,但它不起作用:

import ImageGallery: any from 'react-image-gallery';
import (ImageGallery as any) from 'react-image-gallery';
import { ImageGallery as any } from 'react-image-gallery';

【问题讨论】:

    标签: reactjs typescript npm


    【解决方案1】:

    首先在此处遵循本指南:https://stackoverflow.com/a/44046969/3850405

    添加了一个名为 react-image-gallery 的新文件夹,其中包含 index.d.ts 文件。内容:

    import * as React from "react";
    
    export class ImageGallery extends React.Component<any, any> {
    
    }
    
    export default ImageGallery;
    

    在此之后一切正常,组件可以正常导入import ImageGallery from 'react-image-gallery';

    【讨论】:

      猜你喜欢
      • 2021-12-20
      • 2020-03-30
      • 2017-05-08
      • 1970-01-01
      • 2022-06-20
      • 2021-07-24
      • 1970-01-01
      • 2019-10-22
      相关资源
      最近更新 更多