-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Allow switch statement type guards (need help..) #2230
Conversation
Hi @bsteephenson, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution! TTYL, MSBOT; |
Regarding test in our repository, all tests are located inside tests/cases. For Typeguard feature, there is a folder in tests/cases/conformance/expressions/typeGuards. You can certainly put your tests in there. I would try to be explicit with naming the test file. By default test file is emitted below ES6 if you would like the test to be emitted as es6 including "// @target: es6" at the first line of the file. and include "ES6" as part of the file name. The test file is simply .ts file. When the test runner runs, it generates corresponding .js (plain javascript file), .type (this file only get generated if the ts file is compiled with no error. '.type' file specifies a type of each variable in each statement) and .errors.txt (only if there are errors). |
case SyntaxKind.CaseClause: | ||
// In a case clause of a switch statement using typeof | ||
var parent = (<CaseClause>node).parent; | ||
if((<SwitchStatement>parent).expression.kind === SyntaxKind.TypeOfExpression){ |
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.
Space after if
.
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.
Make sure the parent is guaranteed to be a switch statement. It is possible that the parser lets through case labels that are not parented by a switch. Please add either an assert or a bailout condition.
To add to what @yuit said, you'll need to ensure that you're writing uniquely named tests. Decent test names for this might be
Feel free to take inspiration for existing tests; in fact, you should be testing at least the same features as are tested in You should be testing with strings like
Fourslash tests would need to be added too. If we actually move further along, we can guide you through this process.
Just as we don't do anything in an I would imagine that this change immediately prompts the need for getting the appropriate union type in the case of a fallthrough, or potentially eliminating accounted cases within the default clause. function f(v: string | number | boolean) {
switch (typeof v) {
case 'number':
// 'v' has type 'number'
case 'string':
// 'v' now has type 'number | string'
break;
default:
// 'v' has type 'boolean'
} So we would have to do some sort of control flow analysis to do this. We could have this change temporarily, but I imagine the behavior might be somewhat surprising. I think we should discuss it further. |
To address the question of permissiveness, please follow the same model as narrowTypeByEquality |
@bsteephenson, this repo is using .editorextension. If you are developing using VS, you would probably want to install editorconfig extension, which automagically take editor formatting configuration from the .editorconfig file and apply to VS. https://visualstudiogallery.msdn.microsoft.com/c8bccfe2-650c-4b42-bc5c-845e21f96328 Extensions are also available for vim, nodepad++, sublime etc. |
This PR seems stale. @bsteephenson please refresh and resubmit if this is still something you still want to pursue. |
Hi. This PR resolves issue #2214 allowing the following code to work
I need some help (I'm kinda new to this..)
Thank you!