Skip to content
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

Check possible undefined on LHS IndexedType for logical OR short-circuit #29794

Closed
wants to merge 2 commits into from

Conversation

collin5
Copy link
Contributor

@collin5 collin5 commented Feb 7, 2019

Fixes #29642

@collin5 collin5 changed the title Check possible undefined on LHS IndexedType for logical OR short-circuit evaluation Check possible undefined on LHS IndexedType for logical OR short-circuit Feb 7, 2019
@RyanCavanaugh RyanCavanaugh requested a review from rbuckton March 13, 2019 21:20
/**
* Returns true if any of the items in array satisfies predicate 'if provided' or is truthy
*/
export function any<T>(array: ReadonlyArray<T>, predicate?: (i: any) => boolean): boolean {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is built-in; it's Array#some

@@ -22763,6 +22763,10 @@ namespace ts {
getUnionType([extractDefinitelyFalsyTypes(strictNullChecks ? leftType : getBaseTypeOfLiteralType(rightType)), rightType]) :
leftType;
case SyntaxKind.BarBarToken:
leftType = leftType.flags & TypeFlags.IndexedAccess ?
any(getPropertiesOfType((<IndexedAccessType>leftType).objectType), prop => !!(prop.flags & SymbolFlags.Optional)) ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is right - checking is any prop in the indexed access's type is optional and then returning never for its type if so seems very wrong - the potential type of the LHS is completely lost here, and it loses the nuance of the individual property accessed. This needs #29317 to be correctly solved - the LHS type should be leftType & not 0 & not null & not undefined & not "", which handles generics appropriately.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that we don't have more tests this change, as-is, breaks is actually a little disturbing.

@sandersn
Copy link
Member

sandersn commented Feb 3, 2020

I'm cleaning up our PR backlog and this one is pretty stale, so I'm going to close it.

The issue this fixes, #7719, is closed as fixed by #10357, so I'm not really sure if it's actually fixed. @collin5 you can consider creating a new PR if it's not. @weswigham, you said this probably needs negated types (from #29317) for a real fix, though. Do you still think that?

@sandersn sandersn closed this Feb 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Object is possibly 'undefined'
4 participants