1

I have a package that I use somewhat like this:

import { something } from 'somewhere';

But then I have another package that I import, and I need to define the same something name which is defined in it.

import myConsts from 'SomewhereElse';
const { something, another } = myConsts;

I get an eslint error (and rightly so) something already defined.

Here's a real example:

import { connect } from 'react-redux';
// following lines from react-native-kontaktio sample code...
import Kontakt from 'react-native-kontaktio';
const { connect, configure, startScanning } = Kontakt;

I tried

import { connect as kontaktConnect, configure, startScanning } from 'react-native-kontaktio'  

but get Possible promise rejection ... (reactNativeKontaktio.connect) is not a function.

If I try to change the

import { connect as reduxConnect } from 'react-redux';

I'll have to change the export as follows. Won't that break my code elsewhere?

// export default connect(mapStateToProps, mapDispatchToProps)(AppMain);
export default reduxConnect(mapStateToProps, mapDispatchToProps)(AppMain);

How can I overcome this? Can I ignore the warning in some cases? There is no Polymorphism in Ecma6 right?


This is NOT a question about two classes with the same name, but about two classes with a method or a constant of the same name. The answer there seems to be applicable here, to use:

// instead of: import myConsts from 'SomewhereElse';
import { something as somethingElse, another } from 'SomewhereElse';

But then, when I use... somethingElse().then(()=> ... I get an error Possible promise rejection ... (SomewhereElse.something) is not a function

This also is NOT a [question about fixing the general is already defined eslint error] (Javascript standardjs - how to fix 'is already defined'?), since I am not talking about writing MY code, but rather how to import and use someone else's two packages when they have this clash problem.

pashute
  • 3,965
  • 3
  • 38
  • 65
  • Why are you not using `import { connect, configure, startScanning }' from react-native-kontaktio'` directly? – Ankit Agarwal Jul 30 '18 at 08:17
  • according to their example. Is this old style? Let me try. but won't that too clash with the redux connect? How will the code know which to connect from. – pashute Jul 30 '18 at 08:23
  • Possible duplicate of [How to import two classes by the same name in javascript/es6?](https://stackoverflow.com/questions/34714947/how-to-import-two-classes-by-the-same-name-in-javascript-es6) – feeela Jul 30 '18 at 08:24
  • @feeela I edited question and explained why not duplicate – pashute Jul 30 '18 at 08:46
  • @AnkitAgarwal but then how will I write in the code and "know" which connect to use? Is there anything in ec6 like namespaces? – pashute Jul 30 '18 at 08:48

1 Answers1

4

No I think you can't ignore the warning, because 2 variables with the same name are present in the same scope.

You may need to import in this way:

import { connect as somethingElse} from 'react-redux';

To avoid to variables with the same name.

I hope it will help you

  • please see the update to my question which refers to this solution. If you can answer my problem in one of the two ways, i'll accept this answer. – pashute Jul 30 '18 at 09:46
  • @pashute _But then, when I use... somethingElse().then(()=> ... I get an error Possible promise rejection ... (SomewhereElse.something) is not a function_ I think (I'm not sure) that as acts like an allias ... Are you sure that the error above is due to the use of 'as' ? – Filipe Doutel Silva Jul 30 '18 at 09:57
  • I'm not sure of anything. But in the end all worked out when I used an alias for the first one (in my case: redux) and left the second const (in my case, for kontaktio) as is. – pashute Jul 31 '18 at 09:54