fredericbarthelet / typebridge

Typescript toolbox for AWS EventBridge
76 stars 10 forks source link

TS2589: Type instantiation is excessively deep and possibly infinite. #5

Closed beanaroo closed 3 years ago

beanaroo commented 3 years ago

This library looks great! Experimenting with a trimmed down version of my schema:

const MyEventPayloadSchema = {
  type: 'object',
  properties: {
    body: {
      type: 'object',
    },
    correlationId: {
      format: 'uuid',
      type: 'string',
    },
    eventId: {
      format: 'uuid',
      type: 'string',
    },
    eventType: {
      type: 'string',
    },
    eventVersion: {
      format: 'date',
      type: 'string',
    },
    parentEventId: {
      type: 'string',
    },
  },
  required: [
    'eventId',
    'eventVersion',
    'correlationId',
    'eventType',
    'parentEventId',
    'body',
  ],
};

export const MyEvent = new Event({
  name: 'MyEvent',
  bus: MyBus,
  schema: MyEventPayloadSchema,
  source: 'mySource',
});

TypeScript 4.2.2 compilation fails with:

error TS2589: Type instantiation is excessively deep and possibly infinite.

 41 export const MyEvent = new Event({
                           ~~~~~~~~~~~
 42   name: 'MyEvent',
    ~~~~~~~~~~~~~~~~~~
... 
 45   source: 'mySource',
    ~~~~~~~~~~~~~~~~~~~~~
tsconfig.json ```json { "compilerOptions": { "declaration": true, "lib": [ "ESNext" ], "target": "ES2019", "module": "CommonJS", "outDir": "lib", "strict": true, "noImplicitAny": false, "moduleResolution": "node", "esModuleInterop": true, "skipLibCheck": true, }, "include": [ "src/**/*.ts", ".eslintrc.js" ] } ```
beanaroo commented 3 years ago

I realised a very important detail from the documentation that I missed:

const MyEventPayloadSchema = {...} as const; 

The const assertion is required to avoid the above error.

beanaroo commented 3 years ago

What I originally tried to do was import the schema directly from a json file as produced by an API Designer (Stoplight Studio).

This results in the same error but sadly, there does not appear to be a const assertion for this type of import (yet):

https://github.com/microsoft/TypeScript/issues/32063