Skip to content
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

Fixed type narrowing in switch statements with parenthesized expressions #56035

Merged
merged 1 commit into from
Oct 9, 2023

Conversation

Andarist
Copy link
Contributor

@Andarist Andarist commented Oct 8, 2023

fixes #56030

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Oct 8, 2023
@JoostK
Copy link
Contributor

JoostK commented Oct 9, 2023

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
Contributor Author

Andarist commented Oct 9, 2023

Thanks :)

@jakebailey
Copy link
Member

jakebailey commented Oct 9, 2023

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
Member

Ah, yes, you should be using:

skipParentheses(node, /*excludeJSDocTypeAssertions*/ true)

@jakebailey
Copy link
Member

jakebailey commented Oct 9, 2023

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...)

@mi-na-bot
Copy link

@Andarist I saw this in my email and thought it was Emotion, congratulations on being a TypeScript contributor!!1!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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