Typescript react - Could not find a declaration file for module ''react-materialize'. 'path/to/module-name.js' implicitly has an any type

前端 未结 14 1170
鱼传尺愫
鱼传尺愫 2020-12-05 09:09

I am trying to import components from react-materialize as -

import {Navbar, NavItem} from \'react-materialize\';

But when the webpack is

14条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 09:52

    I had this same problem but not necessarily relating to typescript, so I struggled a bit with these different options. I am making a very basic create-react-app using a specific module react-portal-tooltip, getting similar error:

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

    I tried many steps first - adding various .d.ts files, various npm installs.

    But what eventually worked for me was touch src/declare_modules.d.ts then in src/declare_modules.d.ts:

    declare module "react-portal-tooltip";
    

    and in src/App.js:

    import ToolTip from 'react-portal-tooltip';
    // import './declare_modules.d.ts'
    

    I struggled a bit with the different locations to use this general 'declare module' strategy (I am very much a beginner) so I think this will work with different options but I am included paths for what worked work me.

    I initially thought import './declare_modules.d.ts' was necessary. Although now it seems like it isn't! But I am including the step in case it helps someone.

    This is my first stackoverflow answer so I apologize for the scattered process here and hope it was still helpful! :)

提交回复
热议问题