You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular 12/TypeScript类型错误求助:any类型无法赋值给never类型

Fixing the "Argument of type 'any' is not assignable to parameter of type 'never'" Error in Angular 12

Hey there! Let's break down and fix this TypeScript error you're hitting in your Angular 12 project.

The Root Cause

This error pops up because TypeScript has inferred your recordedChunks array as type never[]—when you initialize an empty array without explicitly defining its type, TypeScript defaults to never[] if it can't guess a more specific type. When you try to push event.data (typed as any) into this array, TypeScript throws an error because never is a type that can't accept any value at all.

Step-by-Step Solution

  1. Explicitly type your recordedChunks array
    Since the data property from the MediaRecorder's ondataavailable event is a Blob, you should declare your array to hold Blob values explicitly:

    // Inside your component class
    recordedChunks: Blob[] = [];
    
  2. Use the native event type instead of a custom { data: any; } type
    TypeScript provides a built-in BlobEvent type for this exact DOM event scenario, which correctly types event.data as Blob—no need for a vague custom type:

    this.recorder.ondataavailable = (event: BlobEvent) => { 
      this.recordedChunks.push(event.data); 
    };
    

Why This Works

By specifying Blob[] for recordedChunks, you tell TypeScript exactly what kind of values the array should store. Pairing this with the native BlobEvent type ensures that event.data perfectly matches the array's expected type, eliminating the type mismatch error entirely.

Quick (But Not Ideal) Workaround

If you need a temporary fix (though I strongly recommend the typed approach above), you can explicitly type recordedChunks as any[] to bypass TypeScript's type checking:

recordedChunks: any[] = [];

Note that this loses the type safety benefits that make TypeScript useful, so it's better to stick with the proper typed solution long-term.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.01 00:42:45