-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
Narrow by clause expressions in switches with true
condition
#53681
Conversation
The TypeScript team hasn't accepted the linked issue #37178. If you can get it accepted, this PR will have a better chance of being reviewed. |
I personally hate this pattern (it feels like the bastard child of JS I guess it could save you from typing a bunch of parentheses... |
Seems to be valid js tho https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch#an_alternative_to_if...else_chains Supporting it is nice 👌 |
To be fair, the bar is usually a wee bit higher than "it works in JS" 😉 but yes, I agree that if people are regularly writing code like this, it's nice to support it. |
Is the issue actually accepted? |
(Code looks good, though.) |
Technically it doesnt have the corresponding label - if that’s what you are asking about. This feature was requested multiple times throughout the years and its a pattern mentioned in MDN docs so i jumped the gun on it and just implemented it as an excercise to learn more about CFA implementation in the compiler |
src/compiler/checker.ts
Outdated
const clause = flow.switchStatement.caseBlock.clauses.find((_, index) => index === flow.clauseStart); | ||
const clauseExpression = clause && clause.kind === SyntaxKind.CaseClause ? clause.expression : undefined; | ||
if (clauseExpression) { | ||
type = narrowType(type, clauseExpression, /*assumeTrue*/ true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should probably use narrowTypeByEquality
here or something and add corresponding tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I re-examined this and I don't think this is needed anyhow here.
@typescript-bot perf test this |
Heya @jakebailey, I've started to run the regular perf test suite on this PR at 7bff88a. You can monitor the build here. Update: The results are in! |
@jakebailey Here they are:
CompilerComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
tsserverComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
StartupComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
I came back to this after a while and I think this is actually ready to be reviewed. |
@typescript-bot test top200 |
Heya @jakebailey, I've started to run the tarball bundle task on this PR at db28e0c. You can monitor the build here. |
Heya @jakebailey, I've started to run the diff-based user code test suite on this PR at db28e0c. You can monitor the build here. Update: The results are in! |
Heya @jakebailey, I've started to run the regular perf test suite on this PR at db28e0c. You can monitor the build here. Update: The results are in! |
Heya @jakebailey, I've started to run the diff-based top-repos suite on this PR at db28e0c. You can monitor the build here. Update: The results are in! |
Heya @jakebailey, I've started to run the parallelized Definitely Typed test suite on this PR at db28e0c. You can monitor the build here. |
Heya @jakebailey, I've started to run the diff-based user code test suite (tsserver) on this PR at db28e0c. You can monitor the build here. Update: The results are in! |
Heya @jakebailey, I've started to run the diff-based top-repos suite (tsserver) on this PR at db28e0c. You can monitor the build here. Update: The results are in! |
Hey @jakebailey, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM so long as everything comes back clean.
@jakebailey Here are the results of running the user test suite comparing Everything looks good! |
@jakebailey Here are the results of running the user test suite comparing There were infrastructure failures potentially unrelated to your change:
Otherwise... Everything looks good! |
@jakebailey Here they are:
CompilerComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
tsserverComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
StartupComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
@typescript-bot run dt |
Heya @jakebailey, I've started to run the parallelized Definitely Typed test suite on this PR at db28e0c. You can monitor the build here. |
@jakebailey Here are the results of running the top-repos suite comparing Everything looks good! |
1 similar comment
@jakebailey Here are the results of running the top-repos suite comparing Everything looks good! |
@typescript-bot run dt |
Heya @jakebailey, I've started to run the parallelized Definitely Typed test suite on this PR at db28e0c. You can monitor the build here. Update: The results are in! |
Hey @jakebailey, the results of running the DT tests are ready. |
Well done @Andarist, thank you! |
fixes #37178