Search code examples
javascripttypescriptcoding-stylecode-formattingprettier

Prettier breaks line in function definition when it has the type of returned function


I have this function:

// before and after prettier

const foo = (a: number) => (b: number) => {
  return a + b
}

If I run prettier it will leave everything as is (which is desired behaviour for me).

When I add type of returned function though it breaks a like for some reason.

// before prettier

type NestedFuncType = (b: number) => number

const foo = (a: number): NestedFuncType => b => {
  return a + b
}

// after prettier
type NestedFuncType = (b: number) => number

const foo =
  (a: number): NestedFuncType =>
  (b) => {
    return a + b
  }

Is there anything I can do to prevent line breaks? My .prettierc:

{
  "printWidth": 120,
  "semi": false,
  "singleQuote": true,
  "trailingComma": "es5"
}

Thanks.


Solution

  • Prettier is opinionated, on purpose. You cannot change the way it formats your code, aside from a few exceptions and looking at their options doc, it does not mention any option to customize the mentioned behavoir. You might consider dropping prettier and using TS ESLint instead.