Skip to content

SwitchOnExtends instead of nested-ternary #1386

@salisbury-espinosa

Description

@salisbury-espinosa

Type description + examples

Nested ternary condition inside Typescript type looks awkward and unreadable
there is even a eslint rule
https://eslint.org/docs/latest/rules/no-nested-ternary

maybe it makes sense to do it SwitchOnExtends with more declarative form:

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

for example converting =>

type KindOf<T> = SwitchOnExtends<
	[string, 'string'],
	[number, 'boolean'],
	[boolean, 'string'],
	[any[], 'array'],
	never
>

or something like that

Type source

No response

Search existing types and issues first

  • I tried my best to look for it

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions