Skip to content

Rule Change: Make no-nested-ternary rule TypeScript syntax aware #19884

@lumirlumir

Description

@lumirlumir

What rule do you want to change?

no-nested-ternary

What change do you want to make?

Generate more warnings

How do you think the change should be implemented?

A new default behavior

Example code

/* eslint no-nested-ternary: "error" */

// JavaScript code with nested ternary operators

const thing = foo ? bar : baz === qux ? quxx : foobar;
foo ? (baz === qux ? quxx() : foobar()) : bar();

// TypeScript code with nested ternary operators

type KindOf<T> = T extends string
  ? 'string'
  : T extends number
    ? 'number'
    : T extends boolean
      ? 'boolean'
      : T extends any[]
        ? 'array'
        : 'other';

What does the rule currently do for this code?

The no-nested-ternary rule currently doesn’t understand TypeScript syntax, so it fails to flag errors when nested conditional types are used.

Image

That’s because the TypeScript AST differs slightly from the JavaScript AST for ternary operations.

TypeScript AST

Image

What will the rule do after it's changed?

For the reasons mentioned above, I’d like to request adding TypeScript syntax support to the no-nested-ternary rule.

Participation

  • I am willing to submit a pull request to implement this change.

Additional comments

This issue is follow-up to #19173

Metadata

Metadata

Assignees

Labels

acceptedThere is consensus among the team that this change meets the criteria for inclusionenhancementThis change enhances an existing feature of ESLintruleRelates to ESLint's core rules

Type

No type

Projects

Status

Complete

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions