Why does this piece of code fail?
/**
* Write the provided contents to a file in the specified directory.
*/
public writeFile(subPath: string, contents: string | Object) {
// stringify the contents if they are not already
if (contents && typeof contents === 'object') contents = JSON.stringify(contents);
this.file.writeFile(this.path, subPath, contents);
}
With the following error:
Argument of type 'string | Object' is not assignable to parameter of type 'string | ArrayBuffer | Blob'. Type 'Object' is not assignable to type 'string | ArrayBuffer | Blob'. The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? Type 'Object' is not assignable to type 'Blob'. The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? Property 'size' is missing in type 'Object'.
I understand writeFile(...); does not accept an Object for its third property, text. But, I have stringified it in the if statement...
The signature to writeFile(...); looks like this:
/** Write a new file to the desired location.
*
* @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above
* @param {string} fileName path relative to base path
* @param {string | Blob} text content or blob to write
* @param {IWriteOptions} options replace file if set to true. See WriteOptions for more information.
* @returns {Promise<any>} Returns a Promise that resolves to updated file entry or rejects with an error.
*/
writeFile(path: string, fileName: string, text: string | Blob | ArrayBuffer, options?: IWriteOptions): Promise<any>;
The error asks if I meant to use any - I did not, since I want stricter control as I'd like to control what can and cannot be passed to this function.
How do I overcome this?