Skip to content

Switch ExpressionΒ #63569

@Lenmint

Description

@Lenmint

πŸ” Search Terms

Switch, Switch expression

βœ… Viability Checklist

⭐ Suggestion

A switch expression like C# or any language.
So i can avoid using Immediately Invoked Function Expression

πŸ“ƒ Motivating Example

Instead of doing this

type State = 'loading' | 'success' | 'error';

const uiMessage = (() => {
  const state: State = 'success';

  switch (state) {
    case 'loading': return 'Fetching data...';
    case 'success': return 'Data loaded!';
    default:
      return 'Unknown error';
  }
})();

let's do this, like C#

type State = 'loading' | 'success' | 'error';

const uiMessage: string = state switch {
    'loading'  => 'Fetching data...',
    'success' => return 'Data loaded!',
    default => 'Unknown error',
}

### πŸ’» Use Cases

1. What do you want to use this for?
3. What shortcomings exist with current approaches?
4. What workarounds are you using in the meantime?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions