I am New to typescript. In my Durandal application I migrated to VS-2012 to VS-2015 means typescript 0.9 to typescript 1.8.4. After migrated I got so many build errors. I re
The issue is with the typescript definitions. A simple cheat is:
let target: any = e.target; //<-- This (any) will tell compiler to shut up!
let content: string = target.result;
Today this worked for me at TypeScript 2.1.1
interface EventTarget { result: any; }
I had the same issue in angular with a FileReader
.
The solution is rather simple (Typescript has the necessary type). You have to use ProgressEvent<FileReader>
. It can be found im lib.dom.d.ts
in the typescript installation, so it should be globally availabe if you build with
lib: {
"dom"
}
in your tsconfig.json
.
Here is the code where i had to use it:
function read(file: File) {
const fileReader = new FileReader();
fileReader.onloadend = function (e: ProgressEvent<FileReader>) {
const arr = (new Uint8Array(parseInt(e.target.result.toString(), 10))).subarray(0, 4);
var header = "";
for (var i = 0; i < arr.length; i++) {
header += arr[i].toString(16);
}
console.log(header);
// Check the file signature against known types
};
fileReader.readAsArrayBuffer(file);
}
this is a break change of javascript/typescript.
what you will need to do is to just replace "event.target.result" by "this.result".
"this" here refers to the context of interface "MSBaseReader".
below are my implementation excerpt:
let reader = new FileReader();
let profile: TransProfile = new TransProfile();
reader.onload = function(event){
profile.avatar = new Uint8Array(this.result);
}
reader.onerror = function(event){
}
this.photoLib.getPhoto(item)
.then(blob => reader.readAsArrayBuffer(blob))
.then(() => this.doUpload(profile));
"MSBaseReader" interface definition:
interface MSBaseReader {
onabort: (this: MSBaseReader, ev: Event) => any;
onerror: (this: MSBaseReader, ev: ErrorEvent) => any;
onload: (this: MSBaseReader, ev: Event) => any;
onloadend: (this: MSBaseReader, ev: ProgressEvent) => any;
onloadstart: (this: MSBaseReader, ev: Event) => any;
onprogress: (this: MSBaseReader, ev: ProgressEvent) => any;
readonly readyState: number;
readonly result: any;
abort(): void;
readonly DONE: number;
readonly EMPTY: number;
readonly LOADING: number;
addEventListener<K extends keyof MSBaseReaderEventMap>(type: K, listener: (this: MSBaseReader, ev: MSBaseReaderEventMap[K]) => any, useCapture?: boolean): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void;
}
"FileReader" interface definition
interface FileReader extends EventTarget, MSBaseReader {
readonly error: DOMError;
readAsArrayBuffer(blob: Blob): void;
readAsBinaryString(blob: Blob): void;
readAsDataURL(blob: Blob): void;
readAsText(blob: Blob, encoding?: string): void;
addEventListener<K extends keyof MSBaseReaderEventMap>(type: K, listener: (this: FileReader, ev: MSBaseReaderEventMap[K]) => any, useCapture?: boolean): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void;
}
also note that, due to the context change of "this" within "onload()", your class-based definitions are not accessible within "reader.onload = function(event){..."; meaning you can not use "this.class-property" style to address your class properties.
you will have to define local variable. see the definition and usage of "profile" in above excerpt.
Try this.
event.target["result"]
If this error comes
Type 'string | ArrayBuffer' is not assignable to type 'string'. Type 'ArrayBuffer' is not assignable to type 'string'
fileReader.result+' ';//valid
fileReader.result; //invalid