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

2

12

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"

Helianthus answered 13/11, 2019 at 13:28 Comment(0)
R
12

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';
Relay answered 8/1, 2020 at 19:47 Comment(1)
Thank you, this is the right answer. As a general rule, declaring the module in index.d.ts will generally work.Helianthus
C
0

if the issue exist after after the .d.ts file creation, use "noImplicitAny": false, in your tsconfig.json

Chamberlin answered 28/7, 2020 at 5:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.