Manually creating JavaScript block scope

I want to share a small tip regarding the block scope. Specifically when we want to create it on purpose.

Let's see in code:

// scope.ts

const func = (someFunc: () => { message: string }) => {
  const message = "Some message..."

  //❗  error: Cannot redeclare block-scoped variable 'message'.ts(2451
  const { message } = someFunc()

  return message
}

message is a constant that is defined in the func function scope. We can’t destruct the operation argument from someFunc return because there is a naming clash.

The possible quick fix for that might be to create an alias:

// scope.ts

const aliasFunc = (someFunc: () => { message: string }) => {
  const message = "Some message..."

  const { message: someMessage } = someFunc()
  console.log("πŸš€ ~ ", someMessage)

  return message
}

Another way is to use block scope:

// scope.ts

const otherFunc = (someFunc: () => { message: string }) => {
  const message = "Some message..."

  {
    const { message } = someFunc()
    console.log("πŸš€ ~ ", message)
  }

  return message
}

When I need to use variable almost immediately after declaration and don't expect to use it a lot, I prefer to use block scope. In all other situations I use alias.