TypeScript type for a compare function's parameters

I am declaring a function to use on a sort method that takes the value on an array of objects and compares it against another array of objects

const compare = (a, b) => {
  let x = 0
  let y = 0
  RoleIndex.forEach(({ role, id}) => {
    if (role.includes(a.role)) x = id
    if (role.includes(b.role)) y = id
  })

  if (x > y) return 1
  else return -1
})

But I am doing this on TypeScript, I am quite new at TypeScript but I can not believe I have to declare the parameters this way:

const compare = ((a: { role: string}, b: { role: string}) => {
  let x: number = 0
  let y: number = 0
  RoleIndex.forEach(({ role, id}) => {
    if (role.includes(a.role)) x = id
    if (role.includes(b.role)) y = id
  })

  if (x > y) return 1
  else return -1
})

Is there a simpler way?

Here is the whole code of what I am trying to do:

TypeScript Playground

206 thoughts on “TypeScript type for a compare function's parameters”

  1. hello there and thank you for your information – I’ve definitely picked up anything new from right here.
    I did however expertise several technical
    points using this site, since I experienced to reload the website a lot of times
    previous to I could get it to load correctly. I had been wondering if your hosting is OK?

    Not that I’m complaining, but slow loading instances times will often affect your placement
    in google and can damage your high quality score if ads and marketing with Adwords.
    Well I am adding this RSS to my e-mail and can look out for a lot more of
    your respective fascinating content. Ensure that you update this again very soon. http://ciaalis2u.com/

    Reply
  2. i need a loan with bad crediti need a loan fast, i need a loan desperately. can i get loan modification need loan now i own my home outright and need a loan need a loan now please help, 1000 payday cash advance loans, cash advance loans, cash advances, payday advance loans without bank account. Money will spark money management, terms of credit.

    Reply

Leave a Comment