How do I import react-admin in a React Typescript appplication?

假如想象 提交于 2020-07-18 03:56:51

问题


I'm trying to set up a react-admin app in typescript and I can't quite figure out how to import react-admin. It gives me the (simple) error saying

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

@types/react-admin is not a valid package but I couldn't find anyone else complaining about this on github or stackoverflow. Am I missing something? As far as I can see, most things have already been migrated to typescript.

Edit: Found this which actually references the problem with ts. However it's been 5 months since they said "it will take months"


回答1:


Current version of react-admin does not export types definition. To make your project to compile you need to create index.d.ts file and modify tsconfig.json.

├── @types
│   └── react-admin
│       └── index.d.ts
└── tsconfig.json
// tsconfig.json

{
  ...
  "compilerOptions": {
    ...
    "typeRoots": ["./@types"],
    ...
  },
  ...
}
// index.d.ts
declare module 'react-admin';


来源:https://stackoverflow.com/questions/58838216/how-do-i-import-react-admin-in-a-react-typescript-appplication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!