-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
Is there a conflict between @types/node and @types/react-native? #15960
Comments
It’s true, there are conflicts between the two. RN provides some features that people are used to from Node (e.g. In the end, we solved it by removing the Node typings and just sprinkle a few typings here and there where they were really needed, e.g. in our Jest setup which does run on Node: artsy/emission@25f296c#diff-0b93f41d9b1048bf2a2e50b4036a898aR2 |
@bherila I run this bash script whenever I do a full npm install (and in my npm postinstall script so I can deploy to Heroku). It replaces the global definitions in the RN typings with nonsense so everything compiles nicely: https://gist.github.com/rawrmaan/be47e71bd0df3f7493ead6cefd6b400c |
Also, for some reason updating to the latest typings of each, and the latest typescript made the problem go away, although I still feel like it isn't "solved" per se :( But I'll close the issue for the time being. That script also looks helpful, thanks @rawrmaan |
Good to know 👍 |
I'm running into this problem with my react-native project via a transitive dependency ing react-native-code-push, which through a chain of dependencies installs a package developed in TypeScript for node, which installs @types/node as its own dependency. I hacked around this by removing @types/node in my postinstall script. I could probably also use @rawrmaan's hack, but I'm wondering what the "right" way to deal with this would be. Should the package that pulls in @types/node not declare it as a dependency? That would leave types in their own type definition unresolved until the consumer of the package pulls in @types/node. They could declare @types/node as a peerDependency, but then non-node consumers such as myself would get a bogus missing peer warning. Or is this a problem for the react-native-code-push package, which needs to be somehow refactored to eliminate the node dependency? |
I'm still hitting the problem with typescript 2.5.2, @types/react-native 0.48.0, and @types/node 8.0.22. I'm removing the errors by commenting out the declarations in @types/node and @types/react-native one by one (running tsc each time) starting with the top most error. |
This is a huge problem with my app which is cross platform since electron pulls it in. One solution is to structure your project in a way to have the dependencies not conflict (have electron and RN as separate packages) Easier said than done. |
+1 - please fix this. getting with latest everything |
+1 I have the issue with RN 0.48.4 |
Is there a way of maybe getting react native to depend on the node type definition instead of rolling it's own ? |
Alas, RN can’t pull in the node definitions, because RN doesn’t run inside node and so TS wouldn’t be able to tell you that certain APIs are not available. We maintainers of the RN typings are all for accepting a solution, but please note that +1s are not going to help, the RN typings are completely driven by the needs of the maintainers and contributors. |
@dieseldjango Alas, the only way to currently do it is indeed to specify it as a peer dependency and leave it up to user to decide what is right for their project. |
A solution that works for me is to define the types that my project is using in tsconfig.json leaving out the node typing’s like this:
"types": [
"mathjs",
"react",
"react-native",
"react-native-drawer",
"react-native-orientation",
"react-navigation",
"jest"
],
This eliminates the conflict even though node.d.ts is in node_modules @types.
…________________________________
From: Eloy Durán <[email protected]>
Sent: Thursday, November 23, 2017 3:16:07 AM
To: DefinitelyTyped/DefinitelyTyped
Cc: chetmurphy; Comment
Subject: Re: [DefinitelyTyped/DefinitelyTyped] Is there a conflict between @types/node and @types/react-native? (#15960)
Alas, RN can’t pull in the node definitions, because RN doesn’t run inside node and so TS wouldn’t be able to tell you that certain APIs are not available.
We maintainers of the RN typings are all for accepting a solution, but please note that +1s are not going to help, the RN typings are completely driven by the needs of the maintainers and contributors.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub<https://nam03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FDefinitelyTyped%2FDefinitelyTyped%2Fissues%2F15960%23issuecomment-346591312&data=02%7C01%7Cchetmurphy%40hotmail.com%7C6a3e9d1f343a457744da08d532639846%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636470325685626725&sdata=S9QpJsNGFkuNzq05oY3aEmk6nK2BtFUYShhM82Fyv3M%3D&reserved=0>, or mute the thread<https://nam03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAJrbVtbl1lYtWLjLPRVNBXkrNv9Z0dDVks5s5VP2gaJpZM4NAs5U&data=02%7C01%7Cchetmurphy%40hotmail.com%7C6a3e9d1f343a457744da08d532639846%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636470325685626725&sdata=QUhwmieDMMq7HUVbvktVupT5ia6d5%2Bw91YNm5rhenmk%3D&reserved=0>.
|
@chetmurphy thanks that is definitely the cleanest workaround |
@alloy is there a way of including that workaround somehow in the typings ? i.e in documentation or just by having it in a comment at the top of the file |
I don’t really know if it will be read when added as a comment to the top of the file, it won’t automatically show up anywhere. People looking for this specific problem may just find this issue and maybe it’s helpful advice in general that can be added to the wiki maybe? https://github.com/DefinitelyTyped/DefinitelyTyped/wiki cc @andy-ms |
@alloy Maybe we could add this information to the duplicate-var error message itself, since that will usually be caused by conflicting types that were automatically included. See microsoft/TypeScript#19356. CC @mhegazy |
@andy-ms that would be a great compromise for me :) and would have saved many wasted hours. |
@andy-ms Agreed, helpful errors are the best. I’d like to PR that, are you suggesting to add something to the error message like the following?
|
Should check with @mhegazy first since the first PR was reverted. |
It's worth noting that @chetmurphy 's workaround needs to be added in tsconfig.json under compilerOptions like this:
|
Is the TS community really okay with a workaround like this? Seems a bit odd that we need to explicitly list types our projects are using after they've already been installed, being careful to make sure none are missed. |
Additionally, this workaround doesn't seem valid if your app makes use of any environment variables (e.g. |
Quoting @alloy:
Based on this, you shouldn't reference |
@kevinsperrine thanks but that dosnt seem to work for me. It fails before it gets to that point when running |
@danwoodbury Can you simply run |
nope, same error. I can manually find the line within the file it is targeting but the script cant. Very strange |
@danwoodbury Very. Hope you can get it straightened out. sed has different parameters depending on platform, so you might try checking into that. |
yeh odd, Will have a look into it, worst case I will just have to build the fetch commands manually until I can get this figured out. Thanks for the help |
Using @kevinsperrine's post install script works as a good workaround for me. The only thing that you have to take into account is that there are some platform differences with If the |
Consider reopening the issue, it still exists with
I'm using Expo SDK v28.0.0 that is based on React Native 0.55.4 |
There's an open issue here #16825 |
I got the same issue. "react": "16.5.0", |
Also same problem with react native and expo |
Does React Native has custom versions of |
@henrikra Where else should it get those definitions from? It can’t get it from the node types, because the RN environment isn’t node so that shouldn’t be loaded. |
@elnygren You seem to be loading the node types, that is what’s causing the problem. Please try to find a different approach. |
It is common to get node types for example if you want to run your unit tests with TypeScript. In this case you need to use |
I get that, we have that issue too, but the actual RN env (your production env) is not node and so that’s what should take precedence imo. In our case we added a few tiny type defs to our test suite. |
What you are using to run your tests with TypeScript? I am using |
Currently we still use |
I'm using this postinstall script to fix issues with globals.
#!/usr/bin/env bash
DIRNAME=$(dirname "$0")
# React Native declares global types that interfere with @types/node and lib dom.
rm -f $DIRNAME/node_modules/@types/react-native/globals.d.ts
sed -i '' 's|/// <reference path="globals.d.ts" />||' $DIRNAME/node_modules/@types/react-native/index.d.ts
# Namespace the globals
sed -i '' 's|declare global|declare namespace IgnoreTheseGlobals|' $DIRNAME/node_modules/@types/react-native/index.d.ts |
@types/node
and@types/react-native
package and had problems.Definitions by:
inindex.d.ts
) so they can respond.Both of the modules seem to define
require
and this results in some compliation trouble:Any tips/workarounds?
The text was updated successfully, but these errors were encountered: