-
Notifications
You must be signed in to change notification settings - Fork 101
Type narrowing union types via return type of method call #1497
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tk0miya
commented
Feb 11, 2025
dbfc792
to
ccf9ece
Compare
tk0miya
commented
Feb 19, 2025
tk0miya
commented
Feb 19, 2025
ccf9ece
to
f9a4880
Compare
This tries to narrow types unions by the return type of the method call. If the return type of the method is always truthy or falsy, the receiver type will be narrowed on the truthy branch and the falsy branch. This helps to narrow types via method definitions (ex. `#nil?`, `#present?`, `#blank?` and so on).
f9a4880
to
59a7823
Compare
@soutaro Thank you for reviewing. updated now. |
soutaro
approved these changes
Feb 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! 👍
tk0miya
added a commit
to tk0miya/gem_rbs_collection
that referenced
this pull request
Mar 8, 2025
This updates the return types of `#present?` and `#blank?` to true, false or bool to narrow the type of receiver. Since 1.10, Steep can narrow the type of receiver via the return type of the method when the type of receiver is union. For example, a receiver typed as will be narrowed to `String` on the `if present?` block, and be narrowed to `String | nil` on the `else` block because `String#present?` returns bool and `NilClass#present?` returns false. refs: * https://github.com/soutaro/steep/wiki/Release-Note-1.10 (unreleased yet) * soutaro/steep#1497
tk0miya
added a commit
to tk0miya/gem_rbs_collection
that referenced
this pull request
Mar 8, 2025
This updates the return types of `#present?` and `#blank?` to narrow the type of receiver possibly. Since 1.10, Steep can narrow the type of receiver via the return type of the method when the type of receiver is union. For example, a receiver typed as will be narrowed to `String` on the `if present?` block, and be narrowed to `String | nil` on the `else` block because `String#present?` returns bool and `NilClass#present?` returns false. refs: * https://github.com/soutaro/steep/wiki/Release-Note-1.10 (unreleased yet) * soutaro/steep#1497
tk0miya
added a commit
to tk0miya/gem_rbs_collection
that referenced
this pull request
Mar 8, 2025
This updates the return types of `#present?` and `#blank?` to narrow the type of receiver possibly. Since 1.10, Steep can narrow the type of receiver via the return type of the method when the type of receiver is union. For example, a receiver typed as will be narrowed to `String` on the `if present?` block, and be narrowed to `String | nil` on the `else` block because `String#present?` returns bool and `NilClass#present?` returns false. ```ruby var = ... # String | nil if var.present? # var is narrowed to String else # var is narrowed to String | nil end ``` refs: * https://github.com/soutaro/steep/wiki/Release-Note-1.10 (unreleased yet) * soutaro/steep#1497
github-actions bot
pushed a commit
to ruby/gem_rbs_collection
that referenced
this pull request
Mar 22, 2025
This updates the return types of `#present?` and `#blank?` to narrow the type of receiver possibly. Since 1.10, Steep can narrow the type of receiver via the return type of the method when the type of receiver is union. For example, a receiver typed as will be narrowed to `String` on the `if present?` block, and be narrowed to `String | nil` on the `else` block because `String#present?` returns bool and `NilClass#present?` returns false. ```ruby var = ... # String | nil if var.present? # var is narrowed to String else # var is narrowed to String | nil end ``` refs: * https://github.com/soutaro/steep/wiki/Release-Note-1.10 (unreleased yet) * soutaro/steep#1497
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 tries to narrow types unions by the return type of the method call. If the return type of the method is always truthy or falsy, the receiver type will be narrowed on the truthy branch and the falsy branch.
This helps to narrow types via method definitions (ex.
#nil?
,#present?
,#blank?
and so on).