Skip to content

Fixed type narrowing in switch statements with parenthesized expressions - #56035

Merged
Jake Bailey (jakebailey) merged 1 commit into
microsoft:mainfrom
Andarist:fix/narrow-switch-extra-parens
Oct 9, 2023
Merged

Fixed type narrowing in switch statements with parenthesized expressions#56035
Jake Bailey (jakebailey) merged 1 commit into
microsoft:mainfrom
Andarist:fix/narrow-switch-extra-parens

Conversation

@Andarist

Copy link
Copy Markdown
Contributor

fixes #56030

@typescript-bot TypeScript Bot (typescript-bot) added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Oct 8, 2023
@JoostK

Copy link
Copy Markdown
Contributor

Ah, you beat me to it :) I would have thought it was something like this. Thanks for providing the fix (and the incredible work you do for TypeScript, really)

@Andarist

Copy link
Copy Markdown
Contributor Author

Thanks :)

@jakebailey

Jake Bailey (jakebailey) commented Oct 9, 2023

Copy link
Copy Markdown
Member

I merged this, but I should have run the tests. In light of #56020, is there a problem here in JS code which casts the argument to a switch, e.g.:

switch (/** @type { "a" | "b" } */ (foo)) {
	case "a":
		return;
	default: {
		const x: "b" = foo;
	}
}

@jakebailey

Copy link
Copy Markdown
Member

Ah, yes, you should be using:

skipParentheses(node, /*excludeJSDocTypeAssertions*/ true)

@jakebailey

Jake Bailey (jakebailey) commented Oct 9, 2023

Copy link
Copy Markdown
Member

Hm, I can't seem to make a test case where this fails. I thought this would show a difference but it does not:

// @strict: true
// @noEmit: true
// @checkJs: true
// @allowJs: true

// @filename: index.js

let value = "";

switch (/** @type {"foo" | "bar"} */ (value)) {
  case "bar":
    value;
    break;

  case "foo":
    value;
    break;

  case "invalid":
    value;
    break;
}

(namely, I would have expected value to not narrow...)

@miinabot

Copy link
Copy Markdown

Mateusz Burzyński (@Andarist) I saw this in my email and thought it was Emotion, congratulations on being a TypeScript contributor!!1!

@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 22, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

5 participants