不能将类型'string'分配给类型'ArrayBuffer |ArrayLike< number>|SharedArrayBuffer' [英] Type 'string' is not assignable to type 'ArrayBuffer | ArrayLike<number> | SharedArrayBuffer'

查看:66
本文介绍了不能将类型'string'分配给类型'ArrayBuffer |ArrayLike< number>|SharedArrayBuffer'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 const arr = new Uint8Array(fileReader.result).subarray(0,4); 中出现错误[ts],尤其是在 fileReader.result 中第11行.

I get an error [ts] in const arr = new Uint8Array(fileReader.result).subarray(0, 4); and particularly in fileReader.result in line 11.

'string |类型的参数不能将ArrayBuffer分配给类型为ArrayBuffer |的参数.ArrayLike |SharedArrayBuffer'.
 不能将类型'string'分配给类型'ArrayBuffer |ArrayLike |SharedArrayBuffer".

Argument of type 'string | ArrayBuffer' is not assignable to parameter of type 'ArrayBuffer | ArrayLike | SharedArrayBuffer'.
  Type 'string' is not assignable to type 'ArrayBuffer | ArrayLike | SharedArrayBuffer'.

import { AbstractControl } from '@angular/forms';
import { Observable, Observer } from 'rxjs';

export const mimeType = (
    control: AbstractControl
    ): Promise<{ [key: string]: any }> | Observable<{ [key: string]: any }>  => {
              const file = control.value as File;
              const fileReader = new FileReader();
              const frObs = Observable.create(
                (observer: Observer<{ [key: string]: any }>) => {
                  fileReader.addEventListener('loadend', () => {
                    const arr = new Uint8Array(fileReader.result).subarray(0, 4);
                    let header = '';
                    let isValid = false;
                    for (let i = 0; i < arr.length; i++) {
                      header += arr[i].toString(16);
                    }

你们能分享一个解决方案吗?谢谢

Can you guys please share a solution. Thanks

推荐答案

您可以使用适当的类型转换来摆脱错误:例如

You can get rid of the error with appropriate type castings: e.g.

...而不是...

...instead of...

this.imagePreview = reader.result;

...尝试...

this.imagePreview = <string>.reader.result;

...或...

this.imagePreview = reader.result as string;

...或者代替...

...or instead of...

const arr = new Uint8Array(fileReader.result).subarray(0, 4);

...写...

const arr = new Uint8Array(<ArrayBuffer>fileReader.result).subarray(0, 4);

...或...

const arr = new Uint8Array(fileReader.result as ArrayBuffer).subarray(0, 4);

我希望这会有所帮助.

这篇关于不能将类型'string'分配给类型'ArrayBuffer |ArrayLike&lt; number&gt;|SharedArrayBuffer'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆