12

I'm trying to add Amplify Authentication in my react native project which uses typescript. There is a package given in amplify documentation 'aws-amplify-react-native' which is used as a middleware to authenticate our application. But this package is only supported in projects which are based on javascript. For Typescript it shows an error like

Could not find a declaration file for module 'aws-amplify-react-native'.
Try `npm install @types/aws-amplify-react-native` if it exists or add a new declaration (.d.ts) file containing `declare module 'aws-amplify-react-native';`

There is no package available like '@types/aws-amplify-react-native'

So anyone can help me out of this?

radhika thakkar
  • 149
  • 2
  • 9

2 Answers2

12

Unfortunately there are no defined types at the time of this answer. However, you can invoke @ts-ignore like so

// @ts-ignore
import Amplify, { Auth } from 'aws-amplify';
// @ts-ignore
import awsconfig from './aws-exports';
// @ts-ignore
import { withAuthenticator } from 'aws-amplify-react-native'

Amplify.configure(awsconfig);
Pi God
  • 152
  • 2
  • 8
6

There are no official TypeScript (TS) types available for aws-amplify-react-native at the moment. To suppress the TypeScript warning, you need to define your own TS declaration file (*.d.ts).

To get approximate typings, you can copy this file into your project. These are typings written by the GitHub user dantasfiles. Bear in mind that they are not exact and could be specified more precisely.

NiFi
  • 2,398
  • 1
  • 17
  • 26
  • Sir I am facing the same issues here could you help me I have imported the fire but still don't know how to configure it – George S Mulbah II Jun 30 '21 at 13:07
  • 1
    @GeorgeSMulbahII Once you have the TS declaration file (.d.ts) in your project, you simply need to ensure it's included in your tsconfig.json with [include](https://www.typescriptlang.org/tsconfig#include) and not [excluded](https://www.typescriptlang.org/tsconfig#exclude). The TS compiler can then couple these types to the amplify package based on `declare module 'aws-amplify-react-native'`. – NiFi Jul 13 '21 at 09:48
  • For more information, refer to e.g. this question: https://stackoverflow.com/questions/21247278/about-d-ts-in-typescript. – NiFi Jul 13 '21 at 09:51
  • This is great (and helps a lot) but how is this a stable solution? I guess I'm confused (coming from Haskell) how TypeScript isn't just a mess (in practice): if package implementations and type definitions are separated, the whole thing makes very little sense and solves no actual problems. What am I missing? – orome May 27 '22 at 21:14
  • @orome I guess that the short answer is that this is a JavaScript ecosystem and JS is a dynamically typed language. TypeScript (which is a superset of JS) types can be offered separately or included in an npm package, or, as in this case, not at all. – NiFi May 28 '22 at 13:53