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

Improve how intersections are classified as weak types #60889

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Andarist
Copy link
Contributor

fixes #56995

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Dec 31, 2024
if (!v) {
return 0;
}
result = result > v ? result : v;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The idea behind the fix is that an empty object type should not be considered to be weak on its own. That's comes from the pre-existing requirement for weak types:

resolved.properties.length > 0 && every(resolved.properties, p => !!(p.flags & SymbolFlags.Optional))

However, when it's intersected with a weak type that should not make it, out of a sudden, non-weak. The new logic still maintains the invariant that an intersection of many empty interfaces is not considered to be weak (it should obey the same rules as a single empty interface).

All of that doesn't change the definition put in the existing code comment:

    /**
     * A type is 'weak' if it is an object type with at least one optional property
     * and no required properties, call/construct signatures or index signatures
     */

Actually, the old behavior wasn't enforcing those rules for intersections precisely enough. The proposed adjustment is closer to checking the intersection as a single type with a flat list of properties instead of checking separate types and combining those results "blindly". I think it's more accurate to do it this way because for most purposes an intersection shouldn't be distinguishable from the same type with a "flattened" list of properties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Status: Not started
Development

Successfully merging this pull request may close these issues.

Weak type not rejected when intersected with ThisType
2 participants