microsoft / microsoft/TypeScript

multiple cases with the same value are allowed in switch/case

Open
#53,604 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

🔎 Search Terms

switch / case
same values on multiple cases

🕗 Version & Regression Information

4.9.4

Playground:
https://www.typescriptlang.org/play?#code/FAMwrgdgxgLglgewgAhgUwM4wBTIG4CGANmGgFzJYBOcEA5sgJTIDewyHlA7nDFABa5CJNE1bAAkBKgEMogOQF5FNlOlIMCImgB0RBHVwAiGXOQBGI4wDckqQCMqaAgGtbUgL7A7phfeXialAaWrr6hsgmsqIATFbuDk6uCV52APRplPwIYEQAJsgQCDDI9qLE+lxoeT7RyIoBqlLBEJraegbGvsgAzPF2Eo7ObnZeEl6p6Fi4DUxAA

💻 Code

// we are allowed to use same value on multiple cases.
// only the first one is executed

function test( value: string ) {
    switch( value ) {
		case 'a': {
			console.log( "case 1");
			break;
		}

		case 'b': {
			console.log( "case 2");
			break;
		}

		// should not be allowed
		case 'a': {
			console.log( "case 3");
			break;
		}
	}
}

test( 'a' )
🙁 Actual behavior

the first case is executed

🙂 Expected behavior

an error should be emit to prevent such error

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the TypeScript Playground example in the issue and trace how the compiler checks duplicate switch case values. The work is done when repeated case values produce an error while valid switch cases continue compiling and executing as before; add or update coverage in the relevant compiler tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.