-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Allow nested assignments in type guards #9013
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
//// [typeGuardsNestedAssignments.ts] | ||
|
||
class Foo { | ||
x: string; | ||
} | ||
|
||
declare function getFooOrNull(): Foo | null; | ||
declare function getStringOrNumberOrNull(): string | number | null; | ||
|
||
function f1() { | ||
let foo: Foo | null; | ||
if ((foo = getFooOrNull()) !== null) { | ||
foo; // Foo | ||
} | ||
} | ||
|
||
function f2() { | ||
let foo1: Foo | null; | ||
let foo2: Foo | null; | ||
if ((foo1 = getFooOrNull(), foo2 = foo1) !== null) { | ||
foo1; // Foo | null | ||
foo2; // Foo | ||
} | ||
} | ||
|
||
function f3() { | ||
let obj: Object | null; | ||
if ((obj = getFooOrNull()) instanceof Foo) { | ||
obj; | ||
} | ||
} | ||
|
||
function f4() { | ||
let x: string | number | null; | ||
if (typeof (x = getStringOrNumberOrNull()) === "number") { | ||
x; | ||
} | ||
} | ||
|
||
// Repro from #8851 | ||
|
||
const re = /./g | ||
let match: RegExpExecArray | null | ||
|
||
while ((match = re.exec("xxx")) != null) { | ||
const length = match[1].length + match[2].length | ||
} | ||
|
||
//// [typeGuardsNestedAssignments.js] | ||
var Foo = (function () { | ||
function Foo() { | ||
} | ||
return Foo; | ||
}()); | ||
function f1() { | ||
var foo; | ||
if ((foo = getFooOrNull()) !== null) { | ||
foo; // Foo | ||
} | ||
} | ||
function f2() { | ||
var foo1; | ||
var foo2; | ||
if ((foo1 = getFooOrNull(), foo2 = foo1) !== null) { | ||
foo1; // Foo | null | ||
foo2; // Foo | ||
} | ||
} | ||
function f3() { | ||
var obj; | ||
if ((obj = getFooOrNull()) instanceof Foo) { | ||
obj; | ||
} | ||
} | ||
function f4() { | ||
var x; | ||
if (typeof (x = getStringOrNumberOrNull()) === "number") { | ||
x; | ||
} | ||
} | ||
// Repro from #8851 | ||
var re = /./g; | ||
var match; | ||
while ((match = re.exec("xxx")) != null) { | ||
var length = match[1].length + match[2].length; | ||
} |
113 changes: 113 additions & 0 deletions
113
tests/baselines/reference/typeGuardsNestedAssignments.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
=== tests/cases/conformance/controlFlow/typeGuardsNestedAssignments.ts === | ||
|
||
class Foo { | ||
>Foo : Symbol(Foo, Decl(typeGuardsNestedAssignments.ts, 0, 0)) | ||
|
||
x: string; | ||
>x : Symbol(Foo.x, Decl(typeGuardsNestedAssignments.ts, 1, 11)) | ||
} | ||
|
||
declare function getFooOrNull(): Foo | null; | ||
>getFooOrNull : Symbol(getFooOrNull, Decl(typeGuardsNestedAssignments.ts, 3, 1)) | ||
>Foo : Symbol(Foo, Decl(typeGuardsNestedAssignments.ts, 0, 0)) | ||
|
||
declare function getStringOrNumberOrNull(): string | number | null; | ||
>getStringOrNumberOrNull : Symbol(getStringOrNumberOrNull, Decl(typeGuardsNestedAssignments.ts, 5, 44)) | ||
|
||
function f1() { | ||
>f1 : Symbol(f1, Decl(typeGuardsNestedAssignments.ts, 6, 67)) | ||
|
||
let foo: Foo | null; | ||
>foo : Symbol(foo, Decl(typeGuardsNestedAssignments.ts, 9, 7)) | ||
>Foo : Symbol(Foo, Decl(typeGuardsNestedAssignments.ts, 0, 0)) | ||
|
||
if ((foo = getFooOrNull()) !== null) { | ||
>foo : Symbol(foo, Decl(typeGuardsNestedAssignments.ts, 9, 7)) | ||
>getFooOrNull : Symbol(getFooOrNull, Decl(typeGuardsNestedAssignments.ts, 3, 1)) | ||
|
||
foo; // Foo | ||
>foo : Symbol(foo, Decl(typeGuardsNestedAssignments.ts, 9, 7)) | ||
} | ||
} | ||
|
||
function f2() { | ||
>f2 : Symbol(f2, Decl(typeGuardsNestedAssignments.ts, 13, 1)) | ||
|
||
let foo1: Foo | null; | ||
>foo1 : Symbol(foo1, Decl(typeGuardsNestedAssignments.ts, 16, 7)) | ||
>Foo : Symbol(Foo, Decl(typeGuardsNestedAssignments.ts, 0, 0)) | ||
|
||
let foo2: Foo | null; | ||
>foo2 : Symbol(foo2, Decl(typeGuardsNestedAssignments.ts, 17, 7)) | ||
>Foo : Symbol(Foo, Decl(typeGuardsNestedAssignments.ts, 0, 0)) | ||
|
||
if ((foo1 = getFooOrNull(), foo2 = foo1) !== null) { | ||
>foo1 : Symbol(foo1, Decl(typeGuardsNestedAssignments.ts, 16, 7)) | ||
>getFooOrNull : Symbol(getFooOrNull, Decl(typeGuardsNestedAssignments.ts, 3, 1)) | ||
>foo2 : Symbol(foo2, Decl(typeGuardsNestedAssignments.ts, 17, 7)) | ||
>foo1 : Symbol(foo1, Decl(typeGuardsNestedAssignments.ts, 16, 7)) | ||
|
||
foo1; // Foo | null | ||
>foo1 : Symbol(foo1, Decl(typeGuardsNestedAssignments.ts, 16, 7)) | ||
|
||
foo2; // Foo | ||
>foo2 : Symbol(foo2, Decl(typeGuardsNestedAssignments.ts, 17, 7)) | ||
} | ||
} | ||
|
||
function f3() { | ||
>f3 : Symbol(f3, Decl(typeGuardsNestedAssignments.ts, 22, 1)) | ||
|
||
let obj: Object | null; | ||
>obj : Symbol(obj, Decl(typeGuardsNestedAssignments.ts, 25, 7)) | ||
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) | ||
|
||
if ((obj = getFooOrNull()) instanceof Foo) { | ||
>obj : Symbol(obj, Decl(typeGuardsNestedAssignments.ts, 25, 7)) | ||
>getFooOrNull : Symbol(getFooOrNull, Decl(typeGuardsNestedAssignments.ts, 3, 1)) | ||
>Foo : Symbol(Foo, Decl(typeGuardsNestedAssignments.ts, 0, 0)) | ||
|
||
obj; | ||
>obj : Symbol(obj, Decl(typeGuardsNestedAssignments.ts, 25, 7)) | ||
} | ||
} | ||
|
||
function f4() { | ||
>f4 : Symbol(f4, Decl(typeGuardsNestedAssignments.ts, 29, 1)) | ||
|
||
let x: string | number | null; | ||
>x : Symbol(x, Decl(typeGuardsNestedAssignments.ts, 32, 7)) | ||
|
||
if (typeof (x = getStringOrNumberOrNull()) === "number") { | ||
>x : Symbol(x, Decl(typeGuardsNestedAssignments.ts, 32, 7)) | ||
>getStringOrNumberOrNull : Symbol(getStringOrNumberOrNull, Decl(typeGuardsNestedAssignments.ts, 5, 44)) | ||
|
||
x; | ||
>x : Symbol(x, Decl(typeGuardsNestedAssignments.ts, 32, 7)) | ||
} | ||
} | ||
|
||
// Repro from #8851 | ||
|
||
const re = /./g | ||
>re : Symbol(re, Decl(typeGuardsNestedAssignments.ts, 40, 5)) | ||
|
||
let match: RegExpExecArray | null | ||
>match : Symbol(match, Decl(typeGuardsNestedAssignments.ts, 41, 3)) | ||
>RegExpExecArray : Symbol(RegExpExecArray, Decl(lib.d.ts, --, --)) | ||
|
||
while ((match = re.exec("xxx")) != null) { | ||
>match : Symbol(match, Decl(typeGuardsNestedAssignments.ts, 41, 3)) | ||
>re.exec : Symbol(RegExp.exec, Decl(lib.d.ts, --, --)) | ||
>re : Symbol(re, Decl(typeGuardsNestedAssignments.ts, 40, 5)) | ||
>exec : Symbol(RegExp.exec, Decl(lib.d.ts, --, --)) | ||
|
||
const length = match[1].length + match[2].length | ||
>length : Symbol(length, Decl(typeGuardsNestedAssignments.ts, 44, 9)) | ||
>match[1].length : Symbol(String.length, Decl(lib.d.ts, --, --)) | ||
>match : Symbol(match, Decl(typeGuardsNestedAssignments.ts, 41, 3)) | ||
>length : Symbol(String.length, Decl(lib.d.ts, --, --)) | ||
>match[2].length : Symbol(String.length, Decl(lib.d.ts, --, --)) | ||
>match : Symbol(match, Decl(typeGuardsNestedAssignments.ts, 41, 3)) | ||
>length : Symbol(String.length, Decl(lib.d.ts, --, --)) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Technically you should consider the compound assignment operators here as well (e.g.
+=
,-=
etc)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.
Compound operators aren't included in control flow analysis in our current design, so that would be a separate issue. But as I see it they wouldn't affect the type because they both a read and a write operation (you're basically storing a value of the same type back into the variable). Did you have something in mind?
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.
Nothing other than odd cases like this:
whereas in this case you get a different result
I don't know how often something like that comes up in practice though.