Support type narrowing
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 2.2k
- Forks
- 256
- Avg merge
- 6d 17h
- Merged PRs (30d)
- 37
Description
Ref: https://github.com/soutaro/steep/issues/472
Currently RBS doesn't have any syntax to narrow types in control flow.
So there's no way to achieve the following:
a = [1, nil].sample # a is `Integer | nil`
unless a.nil?
# a is `Integer` here
p a + 1
end
But actually, both Sorbet and Steep treat some special methods to narrow types
Ideally, I think we need to have a syntax considering Ruby gems which extend Object like ActiveSupport's present?.
I'm not very familiar with type system though, I guess the syntax would be similar with TypeScript's type predicates
https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates
class Object[T?]
def nil?: () -> self is nil
end
class Object[T?]
def present?: () -> self is T
end
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the referenced Sorbet and Steep implementations, then inspect the RBS syntax and type-checking areas related to method signatures and control flow. The work is done when a reviewed syntax and behavior for type narrowing, including methods such as nil? and present?, is supported and tested.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100