Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Body type is not assignable to type string error #12

Open
iby opened this issue Oct 21, 2019 · 4 comments
Open

Body type is not assignable to type string error #12

iby opened this issue Oct 21, 2019 · 4 comments

Comments

@iby
Copy link

iby commented Oct 21, 2019

The API Gateway example doesn't compile in TypeScript:

TS2322: Type '{ first_name: string; last_name: string; }' is not assignable to type 'string'.

The body is expected to be a string.

@mgarciadelojo
Copy link

mgarciadelojo commented Nov 12, 2019

Having the same issue over here:

Type '{ first_name: string; last_name: string; }' is not assignable to type 'string'.ts(2322)
index.d.ts(80, 5): The expected type comes from property 'body' which is declared here on type 'APIGatewayProxyEvent'

@martysweet
Copy link

martysweet commented Nov 25, 2019

Looking at the tests for this project, the call to createEvent is defined in a more lax way. This way worked for me over the examples shown in the README.

const event = createEvent("aws:apiGateway", {
body: JSON.stringify({
first_name: "Sam",
last_name: "Smith",
}),
} as any);

@james-gardner
Copy link

Cool but having to cast it as 'any' seems a bit awkward? I've hit a similar issue today and I'm left in a situation where I might as well just mock out the entire event myself and ditch this library.

@joshyork
Copy link

joshyork commented Mar 9, 2021

Casting to any will work but I believe this issue is because the type of the body param isn't taking into account that some of the required params are supplied by the template.

https://github.com/serverless/event-mocks/blob/master/lib/index.ts#L51

body: typeof dictionary[T],

could instead be defined as

body: Partial<typeof dictionary[T]>,

Even then that's not quite accurate since it'll make all of the properties optional. Even better would be to let the body parameter's type be the type for the corresponding event but with the required fields that are provided by the template marked as optional. I think that's pretty tricky to accomplish dynamically since there isn't a native "merge" type that does what I'm suggesting. Perhaps it'd be more realistic to maintain that type manually but that has its own host of problems.

trevor-leach added a commit to trevor-leach/event-mocks that referenced this issue Jun 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants