Sorbet

Sorbet

  • Get started
  • Docs
  • Try
  • Community
  • GitHub
  • Blog

›Type System

Getting Started

  • Overview
  • Adopting Sorbet
  • Tracking Adoption
  • Quick Reference
  • Visual Studio Code
  • TypeScript ↔ Sorbet

Static & Runtime

  • Gradual Type Checking
  • Enabling Static Checks
  • Enabling Runtime Checks
  • RBI Files
  • Command Line Reference
  • Runtime Configuration

Troubleshooting

  • Troubleshooting
  • Why type annotations?
  • FAQ
  • Error Reference

Type System

  • sig
  • Type Annotations (non-sig)
  • T.let, T.cast, T.must, T.bind
  • Class Types (Integer, String)
  • Arrays & Hashes
  • Nilable Types (T.nilable)
  • Union Types (T.any)
  • Flow-Sensitivity (is_a?, nil?)
  • T.type_alias
  • Exhaustiveness (T.absurd)
  • T::Struct
  • T::Enum
  • T.untyped
  • Blocks, Procs, & Lambdas
  • Abstract Classes & Interfaces
  • Final Methods & Classes
  • Override Checking
  • Sealed Classes
  • T.class_of
  • T.self_type
  • T.noreturn
  • T.attached_class
  • Intersection Types (T.all)
  • T::NonForcingConstants

Experimental Features

  • Tuples
  • Shapes
  • Generics
  • Requiring Ancestors
Edit

Intersection Types (T.all)

TODO: This page is still a fragment. Contributions welcome!

Intersection types can be useful to say, after the fact, that the input must implement two specific interfaces.

# typed: true
extend T::Sig

module I1
  def i1; end
end

module I2
  def i2; end
end

class C
  include I1
  include I2
end

class D
  include I1
  include I2
end


sig {params(x: T.all(I1, I2)).void}
def foo(x)
  x.i1
  x.i2
end

foo(C.new)
foo(D.new)

This is useful because we don’t have to know ahead of time all the things that might implement these two interfaces. It also gets around the problem where we’d have to make a third interface, I12, like this:

module I12
  include I1
  include I2
end

and include this interface into C and D (because maybe we don’t have control over what interfaces C and D can include).

→ View on sorbet.run

← T.attached_classT::NonForcingConstants →

© 2022 Stripe · Get started · Docs · Try · Community · Blog · Twitter