【问题标题】:How to solve the error message: Type 'unknown' is not assignable to type 'BlobPart'如何解决错误消息:类型“未知”不可分配给类型“BlobPart”
【发布时间】:2020-12-03 04:17:46
【问题描述】:

我有以下函数调用

this.component.getXML({ format: true }, (error, currentXML) => {
  if (error) {
    console.error(error.message);
  }

  // do something with currentXML
});

但是我想用asyncawait来解决一个任务,为了做到这一点,我做了以下

async function someName() {
  const promise = new Promise((resolve, reject) => {
    this.component.getXML({ format: true }, (error, currentXML) => {
      if (error) {
        reject(error);
      }

      resolve(currentXML);
    });
  });

  try {
    const currentXML = await promise;

    const blob = new Blob([currentXML], { type: "text/xml" });
    const formData = new FormData();

    formData.append("file", blob, "filename.xml");
  } catch (error) {
    console.error(error.message);
  }
}

这行得通。我的问题是,在尝试构建 blob const blob = new Blob([currentXML], { type: "text/xml" }); 时,它给了我这个错误消息

Type 'unknown' is not assignable to type 'BlobPart'
Type '{}' is missing the following properties from type 'Blob': size, type, arrayBuffer, slice, and 2 more.

调用const currentXML = await promise;的输出是一个字符串,我尝试过明确定义但错误依旧

感谢您的评论。

【问题讨论】:

    标签: javascript typescript angular10


    【解决方案1】:

    我必须通过这种方式添加类型Promise<string>来解决这个问题

    const promise: Promise<string> = new Promise((resolve, reject) => {
      this.modeler.saveXML({ format: true }, (error, updatedXML) => {
        if (error) {
          reject(error);
        }
    
        resolve(updatedXML);
      });
    });
    

    【讨论】:

      猜你喜欢
      • 2021-01-28
      • 2022-06-24
      • 2020-11-02
      • 2021-08-30
      • 2021-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多