Aaronius / penpal

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

TypeScript imports with Penpal 5.2.0 #55

Closed lpellegr closed 3 years ago

lpellegr commented 4 years ago

Using Penpal 5.1.1, I was importing types and defining variables in a TypeScript project as follows:

import {AsyncMethodReturns, Connection, TCallSender, connectToParent} from 'penpal';

protected parentConnection: Connection<TCallSender>;
protected parent: AsyncMethodReturns<TCallSender>;

this.parentConnection = connectToParent({...});
this.parent = await this.parentConnection.promise;

Unfortunately, using Penpal 5.2.0, I cannot find the equivalent.

It seems TCallSender was replaced by CallSender and most types need to be imported from the lib folder:

import {AsyncMethodReturns, CallSender} from 'penpal/lib/types';

My main issue is about Connection which seems not exported. How to import it?

Aaronius commented 4 years ago

@marlon-tucker, what do you make of this? I don't understand how @lpellegr was able to import types with import {AsyncMethodReturns, Connection, TCallSender, connectToParent} from 'penpal';

@lpellegr, can you still import connectToParent with this: import { connectToParent } from 'penpal';

lpellegr commented 4 years ago

@lpellegr, can you still import connectToParent with this: import { connectToParent } from 'penpal';

Yes, this is still possible but no longer AsyncMethodReturns, Connection and TCallSender.

marlon-tucker commented 4 years ago

I don't understand how @lpellegr was able to import types with import {AsyncMethodReturns, Connection, TCallSender, connectToParent} from 'penpal';

I'm equally confused, I checked out tag 5.1.1 and built it, and the index.d.ts looks the same as it is in 5.2.0.

I hadn't realized the package.json file didn't have a types entry before 5.2.0, and I'm not sure what Typescript falls back on if it doesn't find that entry - I would expect it's something to do with that.

Although saying that, in our code we have import { Methods, AsyncMethodReturns } from 'penpal/lib/types'; which is what the language service auto imported.

lpellegr commented 4 years ago

@marlon-tucker How do you import Connection? I don't see it exported in penpal/lib/types.

marlon-tucker commented 4 years ago

That has been something I've been meaning to submit a PR for actually.

Both the return types for connectToParent and connectToChild are not exported, which meant we couldn't store the result as a strongly typed class member (we needed to keep track of it so we could dispose it properly in the future).

I hacked around that by defining a interface with a destroy function.

I just noticed you also seem to be importing TCallSender? That's a generic type argument for the connectToParent function, so that should have never been importable.

But to answer your question we don't import Connection currently, but we would if we could.

Aaronius commented 3 years ago

@marlon-tucker, a PR for Connection would be great! I'm going to close out this issue and create a new one for Connection.