簡體   English   中英

打字稿編譯錯誤: <x> 不可分配給類型 <y>

[英]Typescript compilation error: <x> is not assignable to type <y>

當我嘗試分配

let blob:Blob = {blobParts: [content]};

內容是json。

lib.d.ts中將Blob聲明為

  interface Blob {
      readonly size: number;
      readonly type: string;
      msClose(): void;
      msDetachStream(): any;
      slice(start?: number, end?: number, contentType?: string): Blob;
  }

  declare var Blob: {
    prototype: Blob;
    new (blobParts?: any[], options?: BlobPropertyBag): Blob;
  };

它給我以下錯誤:

Type '{ blobParts: any[]; }' is not assignable to type 'Blob'. Object literal may only specify known properties, and 'blobParts' does not exist in type 'Blob'.

我已經多次驗證了blobParts的拼寫

您應該使用new關鍵字構造Blob ...

let blob: Blob = new Blob([content]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM