Skip to content

Using aws-amplify-react-native with Typescript? #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kevinsperrine opened this issue Feb 13, 2018 · 6 comments
Closed

Using aws-amplify-react-native with Typescript? #281

kevinsperrine opened this issue Feb 13, 2018 · 6 comments
Labels
feature-request Request a new feature

Comments

@kevinsperrine
Copy link

kevinsperrine commented Feb 13, 2018

Is it possible to use aws-amplify-react-native with Typescript? If I don't include the @types/node definitions I get lots of errors about 'stream', 'Buffer', 'http', and other node types missing, but If I include the @types/node definitions then I get a duplicate 'require' error. What's the work-around here? I know RN isn't a full node environment, so I'm not even sure exactly which types are available to the aws-sdk.

# without @types/node
node_modules/aws-sdk/clients/acm.d.ts(108,37): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/acm.d.ts(110,38): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/acm.d.ts(401,32): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/apigateway.d.ts(1146,23): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/appsync.d.ts(240,23): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/clouddirectory.d.ts(1469,38): error TS2304: Cannot find name 'Buffer'.
node_modules/aws-sdk/clients/cloudsearchdomain.d.ts(7,24): error TS2307: Cannot find module 'stream'.
node_modules/aws-sdk/clients/cloudsearchdomain.d.ts(42,23): error TS2304: Cannot find name 'Buffer'.
# ... truncated for brevity.
# with @types/node
node_modules/@types/node/index.d.ts(140,13): error TS2300: Duplicate identifier 'require'.
node_modules/@types/react-native/index.d.ts(8630,14): error TS2300: Duplicate identifier 'require'.
@powerful23
Copy link
Contributor

@kevinsperrine so you are using typescript to develop a react native app? Could you give more details about how you are using the library with Typescript?

@kevinsperrine
Copy link
Author

@powerful23 Yes, exactly. I've created a minimum repo here https://github.com/kevinsperrine/aws-amplify-react-native-typescript-test. I created it with:

create-react-native-app amplify-test --scripts-version=react-native-scripts-ts
cd amplify-test/
yarn add aws-amplify{,-react-native}
yarn upgrade-interactive --latest
yarn eject

So, I'm seemingly stuck because both @types/node and @types/react-native define their own version of require. The only solution I see is to use skipLibCheck in tsconfig.json, or copy out my own minimal @node/types that are needed by the aws-sdk. Cross reference: DefinitelyTyped/DefinitelyTyped#15960

@kevinsperrine
Copy link
Author

FWIW: I just added a postinstall script package.json to comment out the Node require definition and use react-native's version instead since that's the environment I'm actually working in.

#!/bin/sh
# postinstall.sh

# TL;DR node require() and react-native require() types conflict, so I'm
# commenting out the node type definition since we're in a RN env.
# https://github.com/DefinitelyTyped/DefinitelyTyped/issues/15960
# https://github.com/aws/aws-amplify/issues/281
# https://github.com/aws/aws-sdk-js/issues/1926
sed -i '' "s/\(^declare var require: NodeRequire;\)/\/\/\1/g" node_modules/\@types/node/index.d.ts

@pynner
Copy link

pynner commented Mar 8, 2018

Have you made any progress on this issue? Facing same thing

@rasmus-storjohann-PG
Copy link

On Ubuntu I needed to tweak the sed call

sed -i -e "s/\(^declare var require: NodeRequire;\)/\/\/\1/g" node_modules/\@types/node/index.d.ts

@mlabieniec mlabieniec added the feature-request Request a new feature label Apr 2, 2018
@github-actions
Copy link

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request Request a new feature
Projects
None yet
Development

No branches or pull requests

5 participants