Improve checking of in operator combined with noUncheckedIndexedAccess#50936
Improve checking of in operator combined with noUncheckedIndexedAccess#50936Andarist wants to merge 5 commits intomicrosoft:mainfrom
in operator combined with noUncheckedIndexedAccess#50936Conversation
…affected by `noUncheckedIndexedAccess` at all
|
Thanks! This is exactly what's preventing us from using |
…edIndexedAccess # Conflicts: # src/compiler/checker.ts
gabritto
left a comment
There was a problem hiding this comment.
Small thing missing, but other than that, it looks good to me. Thanks 😊
| if (!compilerOptions.noUncheckedIndexedAccess) { | ||
| return filtered; | ||
| } | ||
| return mapType(filtered, t => { |
There was a problem hiding this comment.
I think you should only do this if assumeTrue is true (maybe have the check above be if (!compilerOptions.noUncheckedIndexedAccess || !assumeTrue) ...).
Might be nice to add that to the tests as well, something like:
function f2(obj: Record<string, string>) {
if ("test" in obj) {
return obj.test
}
else {
obj.test // Here it should be `string | undefined` still
}
return "default"
}There was a problem hiding this comment.
great catch! thanks. I've pushed out the fix for this
|
@ahejlsberg could you confirm we're ok with the solution proposed here of adding extra intersections with |
|
First, I very much like the idea of narrowing indexed accesses guarded by I realize that it is possible to get a similar effect using intersections, but it leads to more complex types and is oddly inconsistent with what we do for optional properties. And, really, index signatures are just another form of optional properties. I will put up a PR in the next day or two to show what I mean. We may need to do a bit of optimizing of the logic that detects multiple kinds of |
|
Closing as superseded by #51653 |
follow up to #50666 , mostly piggy-backing on the logic introduced there
fixes #43614
cc @ahejlsberg @andreialecu