1

Typescript code:

My code don't works, i don't understand this error Argument of type '{ src: string; thumb: string; }' is not assignable to parameter of type 'never'

  _albums = [];

  constructor(
    private wowService: NgwWowService,
    private _lightbox: Lightbox
    ) {
    this.wowService.init();

    for (let i = 1; i <= 4; i++) {
      const src = 'assets/desktop/galeria/' + i + '.jpg';
      const thumb = 'assets/desktop/galeria/' + i + '.jpg';
      const album = {
         src: src,
         thumb: thumb
      };
      this._albums.push(album);
    }
  }

  open(index: number): void {
    // open lightbox
    this._lightbox.open(this._albums, index);
  }

  close(): void {
    // close lightbox programmatically
    this._lightbox.close();
  }
  • What type should `_albums` be? You should [annotate the field](https://www.typescriptlang.org/docs/handbook/2/classes.html#fields). Also, please consider making this code a [mcve] suitable for dropping into a standalone IDE like [The TypeScript Playground](https://tsplay.dev/mq8ZdN) where the only error is the one you're asking about – jcalz Jun 28 '21 at 02:25
  • 1
    Does this answer your question? [What is "not assignable to parameter of type never" error in typescript?](https://stackoverflow.com/questions/52423842/what-is-not-assignable-to-parameter-of-type-never-error-in-typescript) – Yong Shun Jun 28 '21 at 02:26
  • no , i don't understand how can i solve this problem; –  Jun 28 '21 at 02:33
  • my code is in Angular 11 –  Jun 28 '21 at 02:35

1 Answers1

2

You need to type _albums. You have defined _albums = [] which typescript infers as an never[] hence if you try to push into the array, you are assigning { src: string; thumb: string; } to never.

Below will work

_albums:{ src: string; thumb: string; }[] = [];
Owen Kelvin
  • 14,054
  • 10
  • 41
  • 74
  • Hi Owen, can you please provide your expertise here.. https://stackoverflow.com/questions/68274003/update-the-count-of-searched-results-and-change-the-selection-based-on-the-numbe/68306975#68306975 – app Jul 09 '21 at 17:00