You need to enable JavaScript to run this app. TypeScript报错:{ src: string; thumb: string; }不可分配给never类型参数
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript报错:{ src: string; thumb: string; }不可分配给never类型参数

问题排查与修复方案

报错核心原因

你声明_albums = []时未指定数组类型,TypeScript会将空数组默认推断为never[]类型,该类型限制数组不允许存入任何值,因此你向数组推入{src: string, thumb: string}结构的对象、以及后续将数组传入lightbox.open方法时都会触发类型不匹配报错。

修复方案

给_albums明确指定符合Lightbox入参要求的数组类型即可,操作方式如下:

  • 优先使用ngx-lightbox库内置的IAlbum类型作为数组元素类型,你可以在导入Lightbox的位置同步导入该类型
  • 若不确定当前依赖的Lightbox版本是否内置IAlbum类型,也可以自己定义对应结构的接口

修正后完整代码

import { Lightbox, IAlbum } from 'ngx-lightbox';

// 无内置IAlbum类型时可以自行定义接口
// interface AlbumItem {
//   src: string;
//   thumb: string;
//   caption?: string;
// }

class 你的组件类名 {
  // 用内置IAlbum类型的写法
  _albums: IAlbum[] = [];
  // 用自定义接口的写法替换上面这行即可
  // _albums: AlbumItem[] = [];

  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();
  }
}

内容的提问来源于stack exchange,提问作者user16262035

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 08:36:03