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
Improve checking of in
operator combined with noUncheckedIndexedAccess
#50936
Closed
Andarist
wants to merge
5
commits into
microsoft:main
from
Andarist:in-operator-noUncheckedIndexedAccess
Closed
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ec7b656
Improve checking of `in` operator combined with `noUncheckedIndexedAc…
Andarist e0bca8c
Removed part of the comment about optional property - as that is not …
Andarist 964fc60
Merge remote-tracking branch 'origin/main' into in-operator-noUncheck…
Andarist 90c9475
Merge remote-tracking branch 'origin/main' into in-operator-noUncheck…
Andarist f1fe5c5
Dont narrow outside of the `assumeTrue`
Andarist File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
39 changes: 39 additions & 0 deletions
39
tests/baselines/reference/inKeywordNarrowingWithNoUncheckedIndexedAccess.symbols
This file contains 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,39 @@ | ||
=== tests/cases/compiler/inKeywordNarrowingWithNoUncheckedIndexedAccess.ts === | ||
declare function invariant(condition: boolean): asserts condition; | ||
>invariant : Symbol(invariant, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 0, 0)) | ||
>condition : Symbol(condition, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 0, 27)) | ||
>condition : Symbol(condition, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 0, 27)) | ||
|
||
function f1(obj: Record<string, string>) { | ||
>f1 : Symbol(f1, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 0, 66)) | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 2, 12)) | ||
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
|
||
invariant("test" in obj); | ||
>invariant : Symbol(invariant, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 0, 0)) | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 2, 12)) | ||
|
||
return obj.test | ||
>obj.test : Symbol(test) | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 2, 12)) | ||
>test : Symbol(test) | ||
} | ||
|
||
function f2(obj: Record<string, string>) { | ||
>f2 : Symbol(f2, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 5, 1)) | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 7, 12)) | ||
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) | ||
|
||
if ("test" in obj) { | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 7, 12)) | ||
|
||
return obj.test | ||
>obj.test : Symbol(test) | ||
>obj : Symbol(obj, Decl(inKeywordNarrowingWithNoUncheckedIndexedAccess.ts, 7, 12)) | ||
>test : Symbol(test) | ||
} | ||
return "default" | ||
} | ||
|
||
|
||
|
42 changes: 42 additions & 0 deletions
42
tests/baselines/reference/inKeywordNarrowingWithNoUncheckedIndexedAccess.types
This file contains 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,42 @@ | ||
=== tests/cases/compiler/inKeywordNarrowingWithNoUncheckedIndexedAccess.ts === | ||
declare function invariant(condition: boolean): asserts condition; | ||
>invariant : (condition: boolean) => asserts condition | ||
>condition : boolean | ||
|
||
function f1(obj: Record<string, string>) { | ||
>f1 : (obj: Record<string, string>) => string | ||
>obj : Record<string, string> | ||
|
||
invariant("test" in obj); | ||
>invariant("test" in obj) : void | ||
>invariant : (condition: boolean) => asserts condition | ||
>"test" in obj : boolean | ||
>"test" : "test" | ||
>obj : Record<string, string> | ||
|
||
return obj.test | ||
>obj.test : string | ||
>obj : Record<string, string> & Record<"test", string> | ||
>test : string | ||
} | ||
|
||
function f2(obj: Record<string, string>) { | ||
>f2 : (obj: Record<string, string>) => string | ||
>obj : Record<string, string> | ||
|
||
if ("test" in obj) { | ||
>"test" in obj : boolean | ||
>"test" : "test" | ||
>obj : Record<string, string> | ||
|
||
return obj.test | ||
>obj.test : string | ||
>obj : Record<string, string> & Record<"test", string> | ||
>test : string | ||
} | ||
return "default" | ||
>"default" : "default" | ||
} | ||
|
||
|
||
|
19 changes: 19 additions & 0 deletions
19
tests/cases/compiler/inKeywordNarrowingWithNoUncheckedIndexedAccess.ts
This file contains 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,19 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
// @noUncheckedIndexedAccess: true | ||
|
||
declare function invariant(condition: boolean): asserts condition; | ||
|
||
function f1(obj: Record<string, string>) { | ||
invariant("test" in obj); | ||
return obj.test | ||
} | ||
|
||
function f2(obj: Record<string, string>) { | ||
if ("test" in obj) { | ||
return obj.test | ||
} | ||
return "default" | ||
} | ||
|
||
|
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.
I think you should only do this if
assumeTrue
is true (maybe have the check above beif (!compilerOptions.noUncheckedIndexedAccess || !assumeTrue) ...
).Might be nice to add that to the tests as well, something like:
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.
great catch! thanks. I've pushed out the fix for this