Type ‘string | ArrayBuffer’ is not assignable to type ‘string’

The error message says it all.

You declare a string type of csv variable.
You then assign string | ArrayBuffer type (of reader.result) to the string type, you just assigned. You cannot. You only can assign string to string.

So, if you 100% sure that reader.result contains string you could assert this:

const csv: string = reader.result as string;

However if you are not sure, do this:

const csv: string | ArrayBuffer = reader.result;
// or simply:
const csv = reader.result; // `string | ArrayBuffer` type is inferred for you

Then you typically should have some check like:

if (typeof csv === 'string') {/*use csv*/}
else {/* use csv.toString() */}

Leave a Comment