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

Fixed an issue with in not being able to be used on narrowed down expression of a generic nullable type #51502

Merged

Conversation

Andarist
Copy link
Contributor

@Andarist Andarist commented Nov 12, 2022

fixes #51501
fixes #51549

Copy link
Member

@ahejlsberg ahejlsberg left a comment

Choose a reason for hiding this comment

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

It's the right fix, but shorter with the suggested change.

const constraint = getBaseConstraintOfType(rightType);
if (!constraint || isEmptyAnonymousObjectType(constraint)) {
error(right, Diagnostics.Type_0_may_represent_a_primitive_value_which_is_not_permitted_as_the_right_operand_of_the_in_operator, typeToString(rightType));
}
Copy link
Member

Choose a reason for hiding this comment

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

Instead of adding these lines of code, I'd suggest modifying hasEmptyObjectIntersection to

    function hasEmptyObjectIntersection(type: Type): boolean {
        return someType(type, t => t === unknownEmptyObjectType || !!(t.flags & TypeFlags.Intersection) && isEmptyAnonymousObjectType(getBaseConstraintOrType(t)));
    }

Accomplishes the same thing with less code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice, I've pushed out the requested change.

@typescript-bot typescript-bot added For Milestone Bug PRs that fix a bug with a specific milestone and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Nov 21, 2022
@ahejlsberg ahejlsberg merged commit 5cd2d97 into microsoft:main Nov 22, 2022
@Andarist Andarist deleted the fix/in-against-nullable-generic-constraint branch November 22, 2022 06:08
@HolgerJeromin
Copy link
Contributor

Could/should this be cherry picked to 4.9 branch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
4 participants