0

I'm having an issue with declaring an interface where a property is an array of an {}.

This is the interface I defined

export interface GlobalEventRegistration {
  'sourceName': string,
  'listenFor': [
    {
      'sourceName': string,
      'eventName': string
    }
  ]
}

My code to implement.

const registration: GlobalEventRegistration = {
    sourceName: this._className,
    listenFor: [
      {
        sourceName: 'MapComponent',
        eventName: 'test'
      },
      {
        sourceName: 'SomeComponent',
        eventName: ''
      }
    ]
  }

The error from the IDE

TS2322: Type '[{ sourceName: string; eventName: string; }, { sourceName: string; eventName: string; }]' is not assignable to type '[{ sourceName: string; eventName: string; }]'.   Source has 2 element(s) but target allows only 1. GlobalEventRegistration.ts(16, 3): The expected type comes from property 'listenFor' which is declared here on type 'GlobalEventRegistration'

I'm not clear on why or how to get it to allow me to have the array of inner {} for the 'listenFor'.

Thanks for looking.

edjm
  • 4,830
  • 7
  • 36
  • 65

1 Answers1

1

just try this directory type interface

export interface GlobalEventRegistration {
  'sourceName': string,
  'listenFor': 
    {
      'sourceName': string,
      'eventName': string
    }[],
}
  • 1
    Thank you. I would not have expected the interface to be the issue. I thought it was in how I was implementing the use of the interface. It fixed my code. Thank you. – edjm Jun 22 '21 at 17:44