Why am I receiving the error message
'event.target.files' is possibly 'null'
on the highlighted line even though there is a null check on the previous line? I understand that I can use the non-null assertion operator, as shown at the end of the code example below, but could someone please clarify this behavior for me?
https://i.sstatic.net/saBeU.png
const handleOnUpload = (event: ChangeEvent<HTMLInputElement>) => {
if (event.target.files === null || event.target.files?.length === 0) return;
if (allowedExtensions.some((extension) => event.target.files![0].type === extension)) {
...
}
...
}
Edit: Interestingly, when I assign the accessed object to a separate variable, no errors are raised:
const handleOnUpload = (event: ChangeEvent<HTMLInputElement>) => {
if (event.target.files === null || event.target.files?.length === 0) return;
const uploadedImage = event.target.files[0];
if (allowedExtensions.some((extension) => uploadedFile.type === extension)) {
...
}
...
}