Skip to content

Switch not narrowed when there is an extra pair of braces. #56030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JeanMeche opened this issue Oct 8, 2023 · 0 comments Β· Fixed by #56035
Closed

Switch not narrowed when there is an extra pair of braces. #56030

JeanMeche opened this issue Oct 8, 2023 · 0 comments Β· Fixed by #56035

Comments

@JeanMeche
Copy link
Contributor

JeanMeche commented Oct 8, 2023

πŸ”Ž Search Terms

  • switch
  • narrowing

πŸ•— Version & Regression Information

This is not a regression, it never worked.

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.2.2#code/JYOwLgpgTgZghgYwgAgEJwM4oN4ChnJgCeADhAFzIDkMA9rVcgD7UBGcUVuAvrrqJFiIUAMXrIIAD0ggAJhjSYc+QqQrU6DANwrNlDGCigA5jt79w0eEkVQJ0iHIXosyPAWJlKVdpx0FfShAAVwBbVmgzPhhgkAQwYFoQZGMIMAA1AAoASkoxWmZbNxUCKDTgqGSQgBtqgEIo3AQkg2QAN2QAXhS0rOydfhhMzLaAOk8IbK7O7p8OKin3dvG1Hj5cAHoN5AB1WigAawxcDAB3YDAEAAtkEZWyReQt5AAeGc6VBCU2efIS5d8-gIyFYZTgBwGBC+rho9Cof2By00QICYIha022wAIrQIAoQLQwMhTvsDidzpcbsMxhNso9nm8Zp9vnNOAjgWNAf9QRBwZDkNCULCGOyCGNkdy0WYgA

πŸ’» Code

interface Base {
  type: 'foo' | 'bar'
}

interface Foo extends Base {
  type: 'foo';
  foo: string;
}

interface Bar extends Base {
  type: 'bar';
  bar: number;
}

function getV(): Foo | Bar {
    return null!;
}

const v = getV();

if((v.type) === 'bar') {
  v.type
}


// Works
switch (v.type) { // <===
  case 'bar':
    v.bar;
    break;

  case 'foo':
    v.foo;
    break;
}

// Does not work
switch ((v.type)) { // <===
  case 'bar':
    v.bar; // KO
    break;

  case 'foo':
    v.foo; // KO
    break;
}

πŸ™ Actual behavior

With extra braces in the switch, narrowing is broken.

switch ((v.type)) { // <=== extra braces
  case 'bar':
    console.log(v.bar);
    break;

  case 'foo':
    console.log(v.foo);
    break;
}

πŸ™‚ Expected behavior

switch ((v.type)) should behave as switch (v.type).

There is no such issue on a if condition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant