8000 Type narrowing union types via return type of method call by tk0miya · Pull Request #1497 · soutaro/steep · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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
merged 1 commit into from
Feb 28, 2025

Conversation

tk0miya
Copy link
Contributor
@tk0miya tk0miya commented Feb 11, 2025

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).

@tk0miya tk0miya force-pushed the type_narrowing/unions branch from dbfc792 to ccf9ece Compare February 16, 2025 03:30
@soutaro soutaro added this to the Steep 1.10 milestone Feb 19, 2025
@tk0miya tk0miya force-pushed the type_narrowing/unions branch from ccf9ece to f9a4880 Compare February 19, 2025 18:49
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).
@tk0miya tk0miya force-pushed the type_narrowing/unions branch from f9a4880 to 59a7823 Compare February 19, 2025 18:54
@tk0miya
Copy link
Contributor Author
tk0miya commented Feb 19, 2025

@soutaro Thank you for reviewing. updated now.

Copy link
Owner
@soutaro soutaro left a comment

Choose a reason for hiding this comment

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

Thanks! 👍

@soutaro soutaro merged commit b7405c7 into soutaro:master Feb 28, 2025
17 checks passed
@tk0miya tk0miya deleted the type_narrowing/unions branch February 28, 2025 02:32
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
@soutaro soutaro added the Released The PR is already included in a published release label Mar 11, 2025
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
Labels
Released The PR is already included in a published release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0