Merged
Conversation
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
This PR improves checking of the
inoperator:key in obj, we now require the type ofkeyto be assignable tostring | number | symbol. Previously we allowedkeyto be of an unconstrained type parameter type.key in obj, we now require the type ofobjto be assignable toobject. Previously we allowedobjto be of an unconstrained generic type (which might be a primitive type, causing an exception to be thrown at runtime).key in obj, wherekeyis of a string literal, numeric literal, or unique symbol type, narrowsobjas follows:keynames a property that exists in some constituent of the type ofobj, the type ofobjis narrowed based on the presence or absence of that property. Previously we only did this for string literal keys.keynames a property that doesn't exist in any constituent of the type ofobj, the type ofobjis narrowed by intersecting withRecord<typeof key, unknown>. Previously we did nothing in this case.Some examples:
I'm marking this PR as fixing #21732 even though it doesn't address the case of
key in objwherekeyis of some generic type. In general, whenkeyis of some non-finite type (likestring), the only effect we can meaningfully reflect following akey in objcheck is thatobj[key]should be a valid expression (of typeunknown), providedkeyandobjare both known not to have been modified since the check. This might be possible to do in the true branch of anifstatement or a ternary operator, but it isn't possible in all control flow scenarios.This PR is a breaking change because of the more accurate checking of the
inoperands and the more consistent narrowing of the right hand operand in control flow analysis.Fixes #21732.
Fixes #50639.