Aaronius / penpal

A promise-based library for securely communicating with iframes via postMessage.
MIT License
389 stars 56 forks source link

TypeScript error: TS2309: An export assignment cannot be used in a module with other exported elements. #16

Closed yoshiokatsuneo closed 6 years ago

yoshiokatsuneo commented 6 years ago

I try to import Penpal like this.

import * as Penpal from 'penpal';

The, I got error:

Error:(43, 3) TS2309: An export assignment cannot be used in a module with other exported elements.


In the following line of index.t.ds.

export = Penpal;


When I import Penpal like this:

import Penpal from 'penpal';


I got error below:

Error:(3, 8) TS1192: Module ''penpal'' has no default export.



penpal 3.0.0 / typescript 2.6.2
yoshiokatsuneo commented 6 years ago

Workaround:

const Penpal = require('penpal').default;
Aaronius commented 6 years ago

Hey @yoshiokatsuneo, do you know how to fix this issue? I don't use TypeScript, so it might take me a while to figure it out. A pull request is welcomed.

Aaronius commented 6 years ago

@paulblyth, could you help out here perchance?

paulblyth commented 6 years ago

Hey, so we use:

import Penpal from 'penpal';

We’re on Penpal ^2.7.0 and Typescript 2.4.0 and we don’t get this error. Let me try with the versions above once I get into the office and I’ll get back to you.

paulblyth commented 6 years ago

I’m on mobile but fairly sure this is the error https://github.com/Aaronius/penpal/commit/8b05093b266c4d6d3f590ddab32b5b726fd3e11e

I don’t think you can have multiple exports in the module (I could be wrong but this is my best guess until I’m at a computer)

paulblyth commented 6 years ago

@yoshiokatsuneo could you please try https://github.com/Aaronius/penpal/pull/17 and let me know if that fixes it for you?

yoshiokatsuneo commented 6 years ago

@paulblyth Unfortunately not...

$ npm install -g typescript
$ mkdir test
$ cd test
$ npm init -y
$ git clone https://github.com/Aaronius/penpal
$ pushd penpal
$ npm install
$ npm run-script build
$ popd
$ cat > test.ts
import Penpal from './penpal';
console.log(Penpal);
$ tsc test.ts
penpal/lib/index.d.ts(33,21): error TS2693: 'Promise' only refers to a type, but is being used as a value here.
penpal/lib/index.d.ts(43,3): error TS2309: An export assignment cannot be used in a module with other exported elements.
test.ts(1,20): error TS2306: File '/private/tmp/test/penpal/lib/index.d.ts' is not a module.

It just works if I just remove index.d.ts ...

paulblyth commented 6 years ago

Thanks @yoshiokatsuneo but I don't see anywhere where you've checked out the PR fix in that code snippet, looks like you're just testing against master? I've tried the steps you've done against my fork/branch and I still get a couple of errors but I'm not convinced they're not red herrings...

I've set up a quick demo here: https://github.com/paulblyth/penpal-ts-test - @Aaronius this might be useful for you too.

Let me know if you run into any issues.

Merry Christmas!

yoshiokatsuneo commented 6 years ago

@paulblyth I just tested using the PR branch in my project. : https://github.com/paulblyth/penpal/tree/fix-16/multiple_exports And, it just works !

Aaronius commented 6 years ago

Fixed in 3.0.1.