类型'string |的参数ArrayBuffer'不能分配给'ArrayBuffer |类型的参数。 SharedArrayBuffer | ArrayLike <number>'

时间:2019-01-10 21:59:32

标签: angular

编译后,会生成上述错误消息。这是我的mime-type.validator.ts文件。

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

    export const mimeType = (
      control: AbstractControl
    ): Promise<{ [key: string]: any }> | Observable<{ [key: string]: any }> => {
      if (typeof(control.value) === 'string') {
        return of(null);
      }
      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);
            }
            switch (header) {
              ...
            }
            if (isValid) {
              observer.next(null);
            } else {
              observer.next({ invalidMimeType: true });
            }
            observer.complete();
          });
          fileReader.readAsArrayBuffer(file);
        }
      );
      return frObs;
    };

我如何摆脱“类型”字符串不能分配给“ ArrayBuffer | SharedArrayBuffer | ArrayLike'。错误消息...谢谢!

1 个答案:

答案 0 :(得分:0)

您可以尝试以下操作:

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