aws / aws/aws-appsync-community

Error when destructuring within function signature (TS)

Open
#324 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
HTML
Stars
507
Forks
37
PR merge metrics
No merged PRs in 30d

Description

Within the AppSync JS runtime environment, I have encountered an unexpected error.

If all of the following are met:
- we have a type containing an optional property
- we have a function that accepts this type as a parameter
- we destructure that parameter within the function signature
- at least one usage of this function does not provide the optional property

The deployment results in an error.

My tsconfig settings are identical to the ones recommended on https://docs.aws.amazon.com/appsync/latest/devguide/resolver-reference-overview-js.html#working-with-typescript but the transpiled js is still troublesome to the js runtime.

### Steps to Reproduce
```ts
export type Params = {
required: string;
optional?: string;
};

export const foo = ({ required, optional }: Params): void => {
util.appendError(`${required}${optional}`, 'log');
};
```

And we call this function without providing the optional property:
```ts
foo({ required: 'bar' });
```

The transpiled js file looks like this:
```js
// src/test.ts
var foo = ({ required, optional }) => {
util.appendError(`${required}${optional}`, "log");
};
export {
foo
};
```

This results in a deployment error every time.

### Workaround

The error goes away if we destructure inside the function rather than the function signature:
```ts
export type Params = {
required: string;
optional?: string;
};

export const foo = (params: Params): void => {
const { required, optional } = params;
util.appendError(`${required}${optional}`, 'log');
};
```

Is this expected behavior?

Contributor guide

Open the contributing guide

Research direction

Start with the reproduced example in src/test.ts and compare the two TypeScript forms shown in the issue. Deploy the generated JavaScript in the AppSync JS runtime using the recommended tsconfig settings, then verify whether the signature-destructuring form fails while the workaround succeeds. Done means the runtime behavior is reproduced and the expected handling or required change is established.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
api, cloud
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.