In my Vue component, I have implemented the following method:
dataURLtoBlob(dataurl: string): Blob {
const arr: string[] = dataurl.split(",");
if (arr) {
if (arr[0]) {
const mime = arr[0].match(/:(.*?);/)[1];
const bstr = atob(arr[1]);
let n = bstr.length;
const u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], { type: mime });
}
}
return new Blob();
},
Despite including if statements to handle potential issues, I am encountering a TypeScript error:
81:34 Object is possibly 'null'.
79 | if (arr) {
80 | if (arr[0]) {
> 81 | const mime = arr[0].match(/:(.*?);/)[1];
| ^
82 | const bstr = atob(arr[1]);
83 | let n = bstr.length;
84 | const u8arr = new Uint8Array(n);
What am I missing here?
Here is the content of my tsconfig.json file:
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"strictNullChecks": true,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": ".",
"types": [
"webpack-env",
"mocha",
"chai"
],
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],
"exclude": [
"node_modules"
]
}