Qubyte Codes

Tip: Find and type narrow an element from an array in ruby and sorbet

Published

Last updated

Recently I had a problem where I had to find the first matching element of an array by type. Ruby provides a method to return the first matching (or nil) element in an array, but sorbet isn't smart enough to type narrow it when the match is related to the type of the element…

Tip: Type narrowing arrays for sorbet in ruby

Published

When working with type systems like TypeScript or Sorbet, type narrowing patterns are a way to handle different types a variable may contain in different branches. For example…