typescript icon indicating copy to clipboard operation
typescript copied to clipboard

Generate alias/intermediary type for function definition

Open coyoteecd opened this issue 2 years ago • 6 comments

@fredericbarthelet

Is it possible to generate an intermediary type/alias for the indexer type of AWS functions? Some plugins expect additional configuration properties in the function definition, and this gives compile errors when using a serverless.ts file.

A good example is https://github.com/go-dima/serverless-plugin-conditional-functions.

My serverless.ts file:

const serverlessConfig: AWS = {
  plugins: [
     'serverless-plugin-conditional-functions',
  ],
 // lots of code
 functions: {
   'func1': {
     handler: 'src/func1.ts',
     events: [/*...eventconfig...*/],
     enabled: 'false'
   }
 }
}

The type definition for functions uses a string indexer, and the type of that indexer is inlined. I want to do something like this:

type ConditionalFunctionsConfig = {
  functions: {
    [key: string]: { enabled?: boolean | string } & ActualFunctionTypeFromAWSType
  }
};

const serverlessConfig: AWS & ConditionalFunctionsConfig = {
  // ...
}

I couldn't figure out a way to get the type starting from the AWS type def, so I'm hoping that if the @serverless/typescript package would provide an alias, then I could use the alias to extend it with additional props as outlined above.

coyoteecd avatar Mar 16 '22 13:03 coyoteecd

This is such a painful thing indeed to have Solution here: https://stackoverflow.com/questions/71278233/how-to-pick-or-access-indexer-index-signature-property-in-a-existing-type-in-typ

colthreepv avatar Mar 23 '22 12:03 colthreepv

@colthreepv thanks for the link, you saved me. What I was missing was the NonNullable<> part, the error message from tsc for AWS['functions'][string] was not very helpful.

coyoteecd avatar Mar 23 '22 16:03 coyoteecd

I think I am in the same boat with the serverless-api-gateway-caching plugin and Typescript. The plugin adds a caching objects to the http event type. Example:

functions: {
    myFunction: {
      handler: ..,
      environment: ..,
      events: [
        {
          http: {
            method: 'get',
            path: ..,
            caching: {
              enabled: true
            }
          } as any,
        },
      ],
    },
  },

The as any is my poor man's solution to get around TS2322. Is there a better way for me than using any here?

reikje avatar Sep 28 '22 20:09 reikje

This is such a painful thing indeed to have Solution here: https://stackoverflow.com/questions/71278233/how-to-pick-or-access-indexer-index-signature-property-in-a-existing-type-in-typ

You saved my life... I've spent 2 hours on this... t would be great to export this automatically from '@serverless/typescript'

export type ServerlessFunction = NonNullable<AWS['functions']>[string]

davulrich avatar Mar 13 '23 11:03 davulrich

That convo was really helpful to extend for a different plugin.

ebisbe avatar May 07 '23 09:05 ebisbe

I think this deserves a mention in the readme.

This is such a painful thing indeed to have Solution here: https://stackoverflow.com/questions/71278233/how-to-pick-or-access-indexer-index-signature-property-in-a-existing-type-in-typ

You saved my life... I've spent 2 hours on this... t would be great to export this automatically from '@serverless/typescript'

export type ServerlessFunction = NonNullable<AWS['functions']>[string]

if someone else needs this:

import { AWS } from '@serverless/typescript';

export type Functions = AWS['functions'];
export type Function = NonNullable<Functions>[string];
export type Events = Function['events'];
export type Event = NonNullable<Events>[number];
export type HttpEvent = Exclude<Extract<Event, { http: unknown }>['http'], string>;

image

franciscocabral avatar Dec 28 '23 06:12 franciscocabral