dprint-plugin-typescript icon indicating copy to clipboard operation
dprint-plugin-typescript copied to clipboard

How can I configurate formatting of ternary operator?

Open doox911 opened this issue 3 years ago • 5 comments

For example:

const value = is_prod 
  ? do() 
  :  is_laptop 
    ? do2()
    : do3();

Language: Javascript, Typescript, Files: Vue, React, jsx and tsx.

doox911 avatar Oct 04 '22 14:10 doox911

Sorry for my delay. Do you mean the operator position? You can use the "conditionalExpression.operatorPosition" configuration for that (see https://dprint.dev/plugins/typescript/config/ and search for "operatorPosition").

Or are you wondering how to indent the second conditional expression? It's not possible to do that at the moment.

dsherret avatar Oct 15 '22 21:10 dsherret

Yes, it is nesting(second conditional expression) that interests me. Then let it be a wish for improvement)!

doox911 avatar Oct 17 '22 06:10 doox911

@dsherret what option can i use to maintain the formatting below?

/**
 * @file Type Definitions - Writable
 * @module tutils/types/Writable
 */

/**
 * Construct a type where all properties of `T` are writable.
 *
 * This is the opposite of {@linkcode Readonly}.
 *
 * @todo examples
 *
 * @template T - Type to evaluate
 */
type Writable<T> = T extends unknown
  ? { -readonly [K in keyof T]: T[K] }
  : never

export type { Writable as default }

i tried changing operatorPosition (and its variants) to maintain, but the new formatting is still ugly. the only workaround is adding dprint-ignore comments, but that of course ignores more than what i am intending to. in my case, i have to add 80+ comments for one project.

edit: i updated the playground link to include more than just the above example. i'm trying to migrate from prettier, but i'm finding that all conditional type formatting results are less than desirable. line break/indentation removal is making my code less readable.

edit 2: i read through #450. from what i understand, that pr may actually fix my issue. is that correct?

unicornware avatar Sep 06 '23 01:09 unicornware