Skip to content

Commit

Permalink
fix: update JS typings
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Jul 8, 2020
1 parent dfaa6d1 commit 20941e6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 19 deletions.
1 change: 1 addition & 0 deletions packages/ERTP/src/issuer.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ import makeAmountMath from './amountMath';
*
* @param {string} allegedName
* @param {string} mathHelpersName
* @returns {IssuerResults}
*/
function produceIssuer(allegedName, mathHelpersName = 'nat') {
assert.typeof(allegedName, 'string');
Expand Down
1 change: 1 addition & 0 deletions packages/assert/src/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ harden(details);
* The optional `optDetails` can be a string for backwards compatibility
* with the nodejs assertion library.
* @param {Details} [optDetails] The details of what was asserted
* @returns {never}
*/
function fail(optDetails = details`Assert failed`) {
if (typeof optDetails === 'string') {
Expand Down
52 changes: 36 additions & 16 deletions packages/eventual-send/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@

type Property = string | number | symbol;

type PromiseLikeOrNot<T> = PromiseLike<T> | T;

type Unpromise<T> = T extends PromiseLikeOrNot<infer U> ? U : T;

type Parameters<T> = T extends (... args: infer T) => any ? T : never;
type ReturnType<T> = T extends (... args: any[]) => infer T ? T : never;

interface EHandler<T> {
get?: (p: T, name: Property) => any;
applyMethod?: (p: T, name?: Property, args: unknown[]) => any;
Expand All @@ -28,18 +35,31 @@ interface HandledPromiseConstructor {

export const HandledPromise: HandledPromiseConstructor;

interface ESingleMethod<R = Promise<unknown>> {
(...args: unknown[]) => R;
readonly [prop: string]: (...args: unknown[]) => R;
/* Types for E proxy calls. */
type ESingleMethod<T> = {
readonly [P in keyof T]: (...args: Parameters<T[P]>) => Promise<ReturnType<T[P]>>;
}

interface ESingleGet<R = Promise<unknown>> {
readonly [prop: string]: R;
type ESingleCall<T> = T extends Function ?
((...args: Parameters<T>) => Promise<ReturnType<T>>) & ESingleMethod<T> :
ESingleMethod<T>;
type ESingleGet<T> = {
readonly [P in keyof T]: Promise<T[P]>;
}

/* Same types for send-only. */
type ESingleMethodOnly<T> = {
readonly [P in keyof T]: (...args: Parameters<T[P]>) => void;
}
type ESingleCallOnly<T> = T extends Function ?
((...args: Parameters<T>) => void) & ESingleMethodOnly<T> :
ESingleMethodOnly<T>;
type ESingleGetOnly<T> = {
readonly [P in keyof T]: void;
}

interface ESendOnly {
(x: unknown): ESingleMethod<void>;
readonly G(x: unknown): ESingleGet<void>;
<T>(x: T): ESingleCallOnly<Unpromise<T>, void>;
readonly G<T>(x: T): ESingleGetOnly<Unpromise<T>>;
}

interface EProxy {
Expand All @@ -49,10 +69,10 @@ interface EProxy {
* whatever 'x' designates (or resolves to) in a future turn, not this
* one.
*
* @param {*} x target for method call
* @returns {ESingleMethod} method call proxy
* @param {*} x target for method/function call
* @returns {ESingleCall} method/function call proxy
*/
(x: unknown): ESingleMethod;
<T>(x: T): ESingleCall<Unpromise<T>>;
/**
* E.G(x) returns a proxy on which you can get arbitrary properties.
* Each of these properties returns a promise for the property. The promise
Expand All @@ -62,16 +82,16 @@ interface EProxy {
* @param {*} x target for property get
* @returns {ESingleGet} property get proxy
*/
readonly G(x: unknown): ESingleGet;
readonly G<T>(x: T): ESingleGet<Unpromise<T>>;

/**
* E.when(x, res, rej) is equivalent to HandledPromise.resolve(x).then(res, rej)
*/
readonly when(
x: unknown,
onfulfilled?: (value: unknown) => unknown | PromiseLike<unknown>,
readonly when<T>(
x: T,
onfulfilled?: (value: Unpromise<T>) => any | PromiseLike<any>,
onrejected?: (reason: any) => PromiseLike<never>,
): Promise<unknown>;
): Promise<any>;

/**
* E.sendOnly returns a proxy similar to E, but for which the results
Expand Down
6 changes: 3 additions & 3 deletions packages/zoe/src/zoe.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ import { makeTables } from './state';
* @param {Keyword} keyword Keyword for added issuer
* @returns {Promise<IssuerRecord>} Issuer is added and ready
*
* * @callback InitPublicAPI
* @callback InitPublicAPI
* Initialize the publicAPI for the contract instance, as stored by Zoe in
* the instanceRecord.
* @param {Object} publicAPI - an object whose methods are the API
Expand All @@ -337,7 +337,7 @@ import { makeTables } from './state';
* within-vat metering of contract code
* @returns {ZoeService} The created Zoe service.
*/
const makeZoe = (additionalEndowments = {}, vatPowers = {}) => {
function makeZoe(additionalEndowments = {}, vatPowers = {}) {
// Zoe maps the inviteHandles to contract offerHook upcalls
const inviteHandleToOfferHook = makeStore();

Expand Down Expand Up @@ -886,6 +886,6 @@ const makeZoe = (additionalEndowments = {}, vatPowers = {}) => {
},
);
return zoeService;
};
}

export { makeZoe };

0 comments on commit 20941e6

Please sign in to comment.