dprint / dprint-plugin-typescript

TypeScript and JavaScript code formatting plugin for dprint.
https://dprint.dev/plugins/typescript
MIT License
258 stars 57 forks source link

Heuristic for line break after "=" on type definitions #674

Open jasonkuhrt opened 2 months ago

jasonkuhrt commented 2 months ago

I find myself doing this often:

// dprint-ignore
type AddFnMerge<$FluentFn extends FnFluent<any>, $State extends State, $MergeFn extends FnMerge> =
  FnCallFluent<
    $FluentFn,
    State<$State['context'], $State['properties'], [...$State['merges'], $MergeFn]>
  >

Dprint would format that as:

type AddFnMerge<$FluentFn extends FnFluent<any>, $State extends State, $MergeFn extends FnMerge> = FnCallFluent<
  $FluentFn,
  State<$State['context'], $State['properties'], [...$State['merges'], $MergeFn]>
>

Note if I insert a newline after type addFnMerge< then this happens (which I approve of):

export type AddFnMerge<
  $FluentFn extends FnFluent<any>,
  $State extends State,
  $MergeFn extends FnMerge,
> = FnCallFluent<
  $FluentFn,
  State<$State['context'], $State['properties'], [...$State['merges'], $MergeFn]>
>

Keeping long type signatures on a single line apart from the type body helps me read the functions. I will resort to the multiline version eventually but there is a threshold before giving into that. For me, at least, vertical space is precious.

Even in a short[er] sig case I still think separating sig from body with a line break is more readable:

// What dprint does
type AddFnMerge<$FluentFn extends FnFluent<any>> = FnCallFluent<
  $FluentFn,
  State<$State['context'], $State['properties'], [...$State['merges'], $MergeFn]>
>
// What I prefer
type AddFnMerge<$FluentFn extends FnFluent<any>> =
  FnCallFluent<
    $FluentFn,
    State<$State['context'], $State['properties'], [...$State['merges'], $MergeFn]>
  >

I think the only time I would want no line break is when there are no type variables.

type ThisIsFine = string

What do you think? If my proposal is not strictly better, then could it at least be an option. E.g. newlineAfterTypeVariablesSignature