aws / aws-appsync-community

The AWS AppSync community
https://aws.amazon.com/appsync
Apache License 2.0
506 stars 32 forks source link

Error when destructuring within function signature (TS) #324

Open berkzorlu opened 11 months ago

berkzorlu commented 11 months ago

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

If all of the following are met:

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

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:

foo({ required: 'bar' });

The transpiled js file looks like this:

// 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:

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?