check if file is an image in typescript

index.ts
function isImageFile(file: File): boolean {
    const acceptedImageTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/bmp', 'image/webp']; // Add other image types as needed

    return acceptedImageTypes.includes(file.type);
}

// Usage
const file = new File([''], 'example.jpg', {type: 'image/jpeg'});
console.log(isImageFile(file)); // Output: true
357 chars
10 lines

related categories

gistlibby LogSnag