Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

59
Vistas
Set Types for HTMLCanvasElement.toBlob callback and URL.createObjectURL

I am using React with Typescript to handle images. I found this great CodeSandbox from react-easy-crop, where they have a utils file using the following snippet:

return new Promise((resolve) => {
    canvas.toBlob((file) => {
        resolve(URL.createObjectURL(file));
    }, 'image/png');
});

This snipped throws the following Typecsript error complaining about the file parameter passed to URL.createObjectURL(file): Argument of type 'Blob | null' is not assignable to parameter of type 'Blob | MediaSource'. Type 'null' is not assignable to type 'Blob | MediaSource'.ts(2345)

As URL.createObjectURL() is expecting a type like Blob | MediaSource, I tried to give this type to the callback function parameter like this:

return new Promise((resolve) => {
    canvas.toBlob((file: Blob | MediaSource) => {
        resolve(URL.createObjectURL(file));
    }, 'image/png');
});

But this throws a new Typescript error: Argument of type '(file: Blob | MediaSource) => void' is not assignable to parameter of type 'BlobCallback'. Types of parameters 'file' and 'blob' are incompatible. Type 'Blob | null' is not assignable to type 'Blob | MediaSource'. Type 'null' is not assignable to type 'Blob | MediaSource'.ts(2345)

Can anyone please advise about how to handle the types for the file parameter? thank you.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It is complaining that file can be null, which would not be a good thing to pass in URL.createObjectURL.

If you know that it can never be null, use an assertion:

return new Promise((resolve) => {
    canvas.toBlob((file) => {
        resolve(URL.createObjectURL(file!));
//                                      ^ '!' used as assertion
    }, 'image/png');
});

Otherwise, handle it appropriately first with an if statement.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda