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

151
Vistas
Typescript: string is assignable to {} Type

Why is this:

const test: {} = "test" // shouldn't this raise "Type 'string' is not assignable to type '{}'." error?
console.log(test)

A valid Typescript code?
Just a moment ago this bit of code caused me to send wrong API calls, because I had something like this:

const getAPIData = async (id: string, filters: NetworkFilters | {} = {})

What I wanted to achieve is the filters prop to have default value of object with no properties, but by my mistake I passed string there and this broke my API calls. Why is it possible? It should raise an error.

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

0

This is sadly one of the edge cases in TS where it's probably a good idea to explain what assignable actually means.

{} is an object without properties, but as such, any object is assignable to it and string is not exception. (string also has methods f.e. toLowerCase).

If you for example declared const test: { toLowerCase: () => string } = "test", you will also not get an error, because toLowerCase is a method of string.

If you said const test: { notAStringMethod: () => string } = "test", then this fails, because notAStringMethod is not a method of string and thus isn't assignable.

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