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

前端 未结 14 1157
鱼传尺愫
鱼传尺愫 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:38

    For my case the issue was that the types were not getting added to package.json file under devDependencies to fix it I ran npm install --save-dev @types/react-redux note the --save-dev

    0 讨论(0)
  • 2020-12-05 09:39

    I had this issue when I added react-router-dom to the new CRA app using typescript. After I added @types/react-router, the issue was fixed.

    0 讨论(0)
  • 2020-12-05 09:43

    A more hacky way is to add eg., in boot.tsx the line

    import './path/declare_modules.d.ts';
    

    with

    declare module 'react-materialize';
    declare module 'react-router';
    declare module 'flux';
    

    in declare_modules.d.ts

    It works but other solutions are better IMO.

    0 讨论(0)
  • 2020-12-05 09:44

    works just fine

    npm install @types/react-materialize
    
    0 讨论(0)
  • 2020-12-05 09:48

    I had a similar error but for me it was react-router. Solved it by installing types for it.

    npm install --save @types/react-router
    

    Error:

    (6,30): error TS7016: Could not find a declaration file for module 'react-router'. '\node_modules\react-router\index.js' implicitly has an 'any' type.
    

    If you would like to disable it site wide you can instead edit tsconfig.json and set noImplicitAny to false.

    0 讨论(0)
  • 2020-12-05 09:48

    Also, this error gets fixed if the package you're trying to use has it's own type file(s) and it's listed it in the package.json typings attribute

    Like so:

    {
      "name": "some-package",
      "version": "X.Y.Z",
      "description": "Yada yada yada",
      "main": "./index.js",
    
      "typings": "./index.d.ts",
    
      "repository": "https://github.com/yadayada.git",
      "author": "John Doe",
      "license": "MIT",
      "private": true
    }
    
    0 讨论(0)
提交回复
热议问题