Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 13.7k
Control flow based type guards#6959
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
42a8efd80f42ed57f5d5c163291f441c999522a99c78ca7b51fb2db6f1c9a82d3dd1fa702edac8670014ecbf9e2c1abed2905a240b8c0e2cf6dc119b3854726f1d84effa8db3844a8c1ff1b2d66c7c6787d2daf9035d973b2e51a32747c98b58eb603f0c283a30fa8fac2f5470a91991a44de5167913d8f15291f26a4d37074a05d2c57ee5927c562a4b91f38673a4609e43021037efa83319158d4cea4c12858219b3f9059b543fc0061c0096c70afa77df546786b33a7575bb9f5b7921efd00d5fbd4ef8a18277cceb0cbea32e2a6592399e13d0f3490979a71985890a678d7646af14a76fe216555ddFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| //// [assignmentTypeNarrowing.ts] | ||
| let x: string | number | boolean | RegExp; | ||
| x = ""; | ||
| x; // string | ||
| [x] = [true]; | ||
| x; // boolean | ||
| [x = ""] = [1]; | ||
| x; // string | number | ||
| ({x} = {x: true}); | ||
| x; // boolean | ||
| ({y: x} = {y: 1}); | ||
| x; // number | ||
| ({x = ""} = {x: true}); | ||
| x; // string | boolean | ||
| ({y: x = /a/} = {y: 1}); | ||
| x; // number | RegExp | ||
| let a: string[]; | ||
| for (x of a) { | ||
| x; // string | ||
| } | ||
| //// [assignmentTypeNarrowing.js] | ||
| var x; | ||
| x = ""; | ||
| x; // string | ||
| x = [true][0]; | ||
| x; // boolean | ||
| _a = [1][0], x = _a === void 0 ? "" : _a; | ||
| x; // string | number | ||
| (_b = { x: true }, x = _b.x, _b); | ||
| x; // boolean | ||
| (_c = { y: 1 }, x = _c.y, _c); | ||
| x; // number | ||
| (_d = { x: true }, _e = _d.x, x = _e === void 0 ? "" : _e, _d); | ||
| x; // string | boolean | ||
| (_f = { y: 1 }, _g = _f.y, x = _g === void 0 ? /a/ : _g, _f); | ||
| x; // number | RegExp | ||
| var a; | ||
| for (var _i = 0, a_1 = a; _i < a_1.length; _i++) { | ||
| x = a_1[_i]; | ||
| x; // string | ||
| } | ||
| var _a, _b, _c, _d, _e, _f, _g; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| === tests/cases/conformance/expressions/assignmentOperator/assignmentTypeNarrowing.ts === | ||
| let x: string | number | boolean | RegExp; | ||
| >x : Symbol(x, Decl(assignmentTypeNarrowing.ts, 0, 3)) | ||
| >RegExp : Symbol(RegExp, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) | ||
| x = ""; | ||
| >x : Symbol(x, Decl(assignmentTypeNarrowing.ts, 0, 3)) | ||
| x; // string | ||
| >x : Symbol(x, Decl(assignmentTypeNarrowing.ts, 0, 3)) | ||
| [x] = [true]; | ||
| >x : Symbol(x, Decl(assignmentTypeNarrowing.ts, 0, 3)) | ||
| x; // boolean | ||
| >x : Symbol(x, Decl(assignmentTypeNarrowing.ts, 0, 3)) | ||
| [x = ""] = [1]; | ||
| >x : Symbol(x, Decl(assignmentTypeNarrowing.ts, 0, 3)) | ||
| x; // string | number | ||
| >x : Symbol(x, Decl(assignmentTypeNarrowing.ts, 0, 3)) | ||
| ({x} = {x: true}); | ||
| >x : Symbol(x, Decl(assignmentTypeNarrowing.ts, 11, 2)) | ||
| >x : Symbol(x, Decl(assignmentTypeNarrowing.ts, 11, 8)) | ||
| x; // boolean | ||
| >x : Symbol(x, Decl(assignmentTypeNarrowing.ts, 0, 3)) | ||
| ({y: x} = {y: 1}); | ||
| >y : Symbol(y, Decl(assignmentTypeNarrowing.ts, 14, 2)) | ||
| >x : Symbol(x, Decl(assignmentTypeNarrowing.ts, 0, 3)) | ||
| >y : Symbol(y, Decl(assignmentTypeNarrowing.ts, 14, 11)) | ||
| x; // number | ||
| >x : Symbol(x, Decl(assignmentTypeNarrowing.ts, 0, 3)) | ||
| ({x = ""} = {x: true}); | ||
| >x : Symbol(x, Decl(assignmentTypeNarrowing.ts, 17, 2)) | ||
| >x : Symbol(x, Decl(assignmentTypeNarrowing.ts, 17, 13)) | ||
| x; // string | boolean | ||
| >x : Symbol(x, Decl(assignmentTypeNarrowing.ts, 0, 3)) | ||
| ({y: x = /a/} = {y: 1}); | ||
| >y : Symbol(y, Decl(assignmentTypeNarrowing.ts, 20, 2)) | ||
| >x : Symbol(x, Decl(assignmentTypeNarrowing.ts, 0, 3)) | ||
| >y : Symbol(y, Decl(assignmentTypeNarrowing.ts, 20, 17)) | ||
| x; // number | RegExp | ||
| >x : Symbol(x, Decl(assignmentTypeNarrowing.ts, 0, 3)) | ||
| let a: string[]; | ||
| >a : Symbol(a, Decl(assignmentTypeNarrowing.ts, 23, 3)) | ||
| for (x of a) { | ||
| >x : Symbol(x, Decl(assignmentTypeNarrowing.ts, 0, 3)) | ||
| >a : Symbol(a, Decl(assignmentTypeNarrowing.ts, 23, 3)) | ||
| x; // string | ||
| >x : Symbol(x, Decl(assignmentTypeNarrowing.ts, 0, 3)) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| === tests/cases/conformance/expressions/assignmentOperator/assignmentTypeNarrowing.ts === | ||
| let x: string | number | boolean | RegExp; | ||
| >x : string | number | boolean | RegExp | ||
| >RegExp : RegExp | ||
| x = ""; | ||
| >x = "" : string | ||
| >x : string | number | boolean | RegExp | ||
| >"" : string | ||
| x; // string | ||
| >x : string | ||
| [x] = [true]; | ||
| >[x] = [true] : [boolean] | ||
| >[x] : [string | number | boolean | RegExp] | ||
| >x : string | number | boolean | RegExp | ||
| >[true] : [boolean] | ||
| >true : boolean | ||
| x; // boolean | ||
| >x : boolean | ||
| [x = ""] = [1]; | ||
| >[x = ""] = [1] : [number] | ||
| >[x = ""] : [string] | ||
| >x = "" : string | ||
| >x : string | number | boolean | RegExp | ||
| >"" : string | ||
| >[1] : [number] | ||
| >1 : number | ||
| x; // string | number | ||
| >x : string | number | ||
| ({x} = {x: true}); | ||
| >({x} = {x: true}) : { x: boolean; } | ||
| >{x} = {x: true} : { x: boolean; } | ||
| >{x} : { x: string | number | boolean | RegExp; } | ||
| >x : string | number | boolean | RegExp | ||
| >{x: true} : { x: boolean; } | ||
| >x : boolean | ||
| >true : boolean | ||
| x; // boolean | ||
| >x : boolean | ||
| ({y: x} = {y: 1}); | ||
| >({y: x} = {y: 1}) : { y: number; } | ||
| >{y: x} = {y: 1} : { y: number; } | ||
| >{y: x} : { y: string | number | boolean | RegExp; } | ||
| >y : string | number | boolean | RegExp | ||
| >x : string | number | boolean | RegExp | ||
| >{y: 1} : { y: number; } | ||
| >y : number | ||
| >1 : number | ||
| x; // number | ||
| >x : number | ||
| ({x = ""} = {x: true}); | ||
| >({x = ""} = {x: true}) : { x?: boolean; } | ||
| >{x = ""} = {x: true} : { x?: boolean; } | ||
| >{x = ""} : { x?: string | number | boolean | RegExp; } | ||
| >x : string | number | boolean | RegExp | ||
| >{x: true} : { x?: boolean; } | ||
| >x : boolean | ||
| >true : boolean | ||
| x; // string | boolean | ||
| >x : string | boolean | ||
| ({y: x = /a/} = {y: 1}); | ||
| >({y: x = /a/} = {y: 1}) : { y?: number; } | ||
| >{y: x = /a/} = {y: 1} : { y?: number; } | ||
| >{y: x = /a/} : { y?: RegExp; } | ||
| >y : RegExp | ||
| >x = /a/ : RegExp | ||
| >x : string | number | boolean | RegExp | ||
| >/a/ : RegExp | ||
| >{y: 1} : { y?: number; } | ||
| >y : number | ||
| >1 : number | ||
| x; // number | RegExp | ||
| >x : number | RegExp | ||
| let a: string[]; | ||
| >a : string[] | ||
| for (x of a) { | ||
| >x : string | number | boolean | RegExp | ||
| >a : string[] | ||
| x; // string | ||
| >x : string | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| //// [controlFlowAssignmentExpression.ts] | ||
| let x: string | boolean | number; | ||
| let obj: any; | ||
| x = ""; | ||
| x = x.length; | ||
| x; // number | ||
| x = true; | ||
| (x = "", obj).foo = (x = x.length); | ||
| x; // number | ||
| //// [controlFlowAssignmentExpression.js] | ||
| var x; | ||
| var obj; | ||
| x = ""; | ||
| x = x.length; | ||
| x; // number | ||
| x = true; | ||
| (x = "", obj).foo = (x = x.length); | ||
| x; // number |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| === tests/cases/conformance/controlFlow/controlFlowAssignmentExpression.ts === | ||
| let x: string | boolean | number; | ||
| >x : Symbol(x, Decl(controlFlowAssignmentExpression.ts, 0, 3)) | ||
| let obj: any; | ||
| >obj : Symbol(obj, Decl(controlFlowAssignmentExpression.ts, 1, 3)) | ||
| x = ""; | ||
| >x : Symbol(x, Decl(controlFlowAssignmentExpression.ts, 0, 3)) | ||
| x = x.length; | ||
| >x : Symbol(x, Decl(controlFlowAssignmentExpression.ts, 0, 3)) | ||
| >x.length : Symbol(String.length, Decl(lib.d.ts, --, --)) | ||
| >x : Symbol(x, Decl(controlFlowAssignmentExpression.ts, 0, 3)) | ||
| >length : Symbol(String.length, Decl(lib.d.ts, --, --)) | ||
| x; // number | ||
| >x : Symbol(x, Decl(controlFlowAssignmentExpression.ts, 0, 3)) | ||
| x = true; | ||
| >x : Symbol(x, Decl(controlFlowAssignmentExpression.ts, 0, 3)) | ||
| (x = "", obj).foo = (x = x.length); | ||
| >x : Symbol(x, Decl(controlFlowAssignmentExpression.ts, 0, 3)) | ||
| >obj : Symbol(obj, Decl(controlFlowAssignmentExpression.ts, 1, 3)) | ||
| >x : Symbol(x, Decl(controlFlowAssignmentExpression.ts, 0, 3)) | ||
| >x.length : Symbol(String.length, Decl(lib.d.ts, --, --)) | ||
| >x : Symbol(x, Decl(controlFlowAssignmentExpression.ts, 0, 3)) | ||
| >length : Symbol(String.length, Decl(lib.d.ts, --, --)) | ||
| x; // number | ||
| >x : Symbol(x, Decl(controlFlowAssignmentExpression.ts, 0, 3)) | ||
Uh oh!
There was an error while loading. Please reload this page.
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 don't see this brand applied anywhere - is there a reason for this?
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.
If a type guard returns false, it is expected that the argument is not of the specified type. However,
isDeclarationNamecan return false whennameis an identifier. This wasn't an issue, asisDeclarationNamewas never used in an if statement with an else. However, with control flow based type guards, this wasn't working anymore. The then-block of an if statement contained a return, thus the statements after the if were the else branch of the if. It might not be the best way to solve this, but it's working..