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

174
Views
JS/TS Difference bewteen function definitions in object?

A specific problem fixed was one of function declaration mismatch. I am wondering, in my specific case, what the difference between

const obj => {
  thing1: () => void //This is type thing1: () => undefined
  thing2(){ return; }  //This is type  thing2(): void
};

I do not see any difference between the two. One is an object attribute that contains a lambda function and the other is a function declared in the object. Why are they different types?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your example code is invalid, but I'm going to assume your meant this:

const obj = {
  thing1: () => undefined,
  thing2() { return },
};

The type of thing1 is:

(property) thing1: () => undefined

This is a property of obj that has a value of a function. That function is an arrow function that returns undefined. Arrow functions capture the value of this from the declaring scope. So this could be anything, from the type only can't tell. However, what this does tell you is that you don't need to be concerned with the value of this since it's bound in the function declaration itself. This means you could break it off and pass it as a callback like:

obj.thing1() // `this` is whatever `this` was when the function was declared.

const fn = obj.thing1
fn() // `this` is whatever `this` was when the function was declared.

thing2 is typed as:

(method) thing2(): void

In this case thing2 is a method of obj that returns a value that should not be used. In reality it's undefined, but as far as type is concerned, it returns an unusable return value.

And here this is not bound for you, and is instead set by calling syntax:

obj.thing2()
// ^ this dot sets the value of `this` in `thing2()`.

If you were to do the same test above here:

obj.thing2() // `this` is `obj`

const fn = obj.thing2
fn() // `this` the global object, which is probably not good.
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!