typescript icon indicating copy to clipboard operation
typescript copied to clipboard

Stream typescript auto-generated definitions

Open fredericbarthelet opened this issue 3 years ago • 1 comments

Not a directly related question but I would appreciate your guidance. Now that type definitions are auto-generated, certain things are much clearer and certain things are not. Please take a look at stream event definition:

I do this:

      events: [
        {
          stream: {
            arn: {
              'Fn::GetAtt': ['MyTable', 'StreamArm'],
            },
            batchSize: 1,
          },
        },
      ],

and serverless fails fast on functions.propagate.events[0].stream.arn': should be string with configValidationMode: 'error'. If I turn off configValidationMode, serverless package fails on EventSourceArn.split is not a function. This has been talked about long time ago here https://github.com/serverless/serverless/issues/2365 and later fixed here https://github.com/serverless/serverless/pull/3111.

The fix was simple - adding an explicit type attribute to the stream event definition. DefinitelyTyped has it (https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/serverless/plugins/aws/provider/awsProvider.d.ts#L408):

    interface Stream {
        arn: string | { [key: string]: any };
        batchSize?: number | string;
        startingPosition?: number | string;
        enabled?: boolean;
        type?: 'dynamodb' | 'kinesis';
    }

but it's lost in auto-generated types:

        {
            stream:
              | AwsArnString
              | (
                  | {
                      arn: AwsCfFunction;
                      [k: string]: unknown;
                    }
                  | {
                      arn: AwsArnString;
                      [k: string]: unknown;
                    }
                );
          }

So my question is - where to submit a PR or an Issue to get back those typed attributes into the generated type definitions? Adding type solved my problem but it would be nice if it was visible on the type def and thus accessible via auto-complete

Thanks!

Originally posted by @pveller in https://github.com/serverless/typescript/issues/38#issuecomment-816753061

fredericbarthelet avatar Apr 13 '21 14:04 fredericbarthelet

Hi @pveller and thanks for reporting your issue. Regarding your issue, the code that will be used for typescript definition generation is https://github.com/serverless/serverless/blob/19805d71eabb68bd7fd4046aa23090ef85fb1c36/lib/plugins/aws/package/compile/events/stream.js#L15-L90

This JSON schema definition can be improved to ensure generated type stills give you information on the expected type property. I give such an exemple in the PR I just opened.

In the future, do not hesitate to open a separate issue with your problem description (which was very clear by the way :) ).

fredericbarthelet avatar Apr 13 '21 14:04 fredericbarthelet