Comparing version 6.2.0 to 6.2.1
{ | ||
"name": "penpal", | ||
"version": "6.2.0", | ||
"version": "6.2.1", | ||
"description": "A promise-based library for communicating with iframes via postMessage.", | ||
@@ -5,0 +5,0 @@ "author": "Aaron Hardy <aaron@aaronhardy.com>", |
@@ -219,2 +219,35 @@ [![npm version](https://badge.fury.io/js/penpal.svg)](https://badge.fury.io/js/penpal) | ||
## TypeScript | ||
When calling `connectToChild` or `connectToParent`, you may pass a generic type argument. This will be used to type the `child` or `parent` object that `connection.promise` is resolved with. This is better explained in code: | ||
```typescript | ||
import { connectToChild } from 'penpal'; | ||
// This interace could be imported from a code library | ||
// that both the parent and child share. | ||
interface ChildApi { | ||
multiply(...args: number[]): number; | ||
} | ||
// Supply the interface as a generic argument. | ||
const connection = connectToChild<ChildApi>({ | ||
iframe: new HTMLIFrameElement(), | ||
}); | ||
// The resulting child object will contain properly | ||
// typed methods. | ||
const child = await connection.promise; | ||
// The result variable is typed as a number. | ||
const result = await child.multiply(1, 3); | ||
``` | ||
The following TypeScript types are also exported as named constants for your use: | ||
- `Connection` | ||
- `Methods` | ||
- `AsyncMethodReturns` | ||
- `CallSender` | ||
- `PenpalError` | ||
## Supported Browsers | ||
@@ -221,0 +254,0 @@ |
131254
268