I am facing a challenge in comparing two arrays, where one array is sourced from a third-party AWS service and its existence cannot be guaranteed.
Despite my efforts to handle potential errors by incorporating return statements in my function calls, I continue to encounter the following error:
Argument of type '(string | undefined)[]' is not assignable to parameter of type 'string[]'. Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.ts(2345)
An additional warning arises when attempting to verify the similarity between the two arrays.
const fileNames = files?.Contents?.filter((content) => content.Key?.endsWith('.docx')).map((content) =>
content.Key?.replace(FOLDER_PATH, '')
);
if (!fileNames || !fileNames.length || fileNames === undefined) {
return;
}
compareFileNames(fileNames, configFiles) // compilation error above
// ...
const compareFileNames = (a: string[], b: string[]) => {
if (a.length !== b.length) return false;
return a.sort().toString() === b.sort().toString(); // Warning Move this array "sort" operation to a separate statement
}
What are the issues at play in this scenario?