event-mocks icon indicating copy to clipboard operation
event-mocks copied to clipboard

Body should be partial

Open danilofuchs opened this issue 5 years ago • 1 comments

Current type definition for createEvent is this:

declare const dictionary: {
    "aws:apiGateway": import("aws-lambda").APIGatewayProxyEvent;
};

export default function createEvent<T extends keyof typeof dictionary, B>(
  eventType: T,
  body: typeof dictionary[T],
): typeof dictionary[T];

When accessing

createEvent("aws:apiGateway", {
    body: JSON.stringify({}),
}

Which gives the following error:

Argument of type '{ body: string; }' is not assignable to parameter of type 'APIGatewayProxyEvent'.
  Type '{ body: string; }' is missing the following properties from type 'APIGatewayProxyEventBase<{ [name: string]: any; }>': headers, multiValueHeaders, httpMethod, isBase64Encoded, and 7 more.

The library merges the body with the template, so the user should be able to provide a deeply partial object.

The type definition should be:

// https://gist.github.com/navix/6c25c15e0a2d3cd0e5bce999e0086fc9
+ type DeepPartial<T> = T extends Function ? T : (T extends object ? { [P in keyof T]?: DeepPartial<T[P]>; } : T); 

export default function createEvent<T extends keyof typeof dictionary, B>(
  eventType: T,
-   body: typeof dictionary[T],
+   body: DeepPartial<typeof dictionary[T]>
): typeof dictionary[T];

danilofuchs avatar Jun 02 '20 16:06 danilofuchs

Having the same error, is it still not fixed?

prabhanjansyn avatar Jan 27 '21 10:01 prabhanjansyn