1

I'm new to typescript and I am questioning a pattern in the codebase I inherited:

export const registerTubesReducer = handleTubeActions

function handleTubeActions(previousState: ITubes, action: TubeAction): ITubes {

versus

export function registerTubesReducer(previousState: ITubes, action: TubeAction): ITubes {

Is there any reason other than style to prefer export const over the plain function export?

3
  • I don't think so. See if anyone else has anything to add. Commented Oct 11, 2018 at 2:17
  • 1
    Possible duplicate of Arrow function vs function declaration / expressions: Are they equivalent / exchangeable? Commented Oct 11, 2018 at 2:24
  • 1
    function & arrow functions treat "this" differently. If you are aware of that then not much to add. I would say the first snipped is bad though because it uses "handleTubeActions" before it is defined. Commented Oct 11, 2018 at 2:27

1 Answer 1

4

There is no major difference in this case. Although the first version can be simplified to

export const registerTubesReducer = (previousState: ITubes, action: TubeAction): ITubes => { ... }

Differences to be aware of between arrow and regular functions.

1) Arrow functions have a lexically bound this which does not change depending on how you invoke the function

2) Arrow functions are not hoisted as they are just variable declarations with a function value. You will not be able to use the function until it is defined or else you will be calling and undefined variable

arrowAdd(1, 2) // arrowAdd will be undefined here
functionAdd(1,2) // All good since function declarations are hoisted

const arrowAdd = (a: number, b: number) => a + b

function functionAdd(a: number, b:number) {
  return a + b
}

3) Arrow functions allow you to omit the extra curly braces if you only want to return a value

There is a canonical answer here for more details about the differences between arrow and regular functions

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.