Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!