Sorbet

Sorbet

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

›Getting Started

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)
  • Generics
  • T::NonForcingConstants

Experimental Features

  • Tuples
  • Shapes
  • Requiring Ancestors
Edit

Overview

Type checking with Sorbet is composed of two key components:

  • srb

    This is the command-line interface to Sorbet. It includes the core type checker, which analyzes a project statically (before the code runs) to report potential mistakes in the code. It also contains utilities to set up a project to work with Sorbet for the first time.

  • sorbet-runtime

    This is the gem that enables adding type annotations to normal Ruby code. It exposes the top-level T namespace and the sig method, which we’ll see more of in Signatures. It also dynamically type checks the code while it runs.

These two components are developed in tandem, and in fact compound each others’ guarantees. Sorbet makes predictions about the runtime, and the runtime enforces those predictions with contracts.

Here’s a taste of what Sorbet can do:

# typed: true
require 'sorbet-runtime'

class A
  extend T::Sig

  sig {params(x: Integer).returns(String)}
  def bar(x)
    x.to_s
  end
end

def main
  A.new.barr(91)   # error: Typo!
  A.new.bar("91")  # error: Type mismatch!
end

→ View on sorbet.run

What’s next?

  • Adopting Sorbet

    Learn how to adopt Sorbet in an existing codebase.

  • Gradual Type Checking and Sorbet

    Learn about how Sorbet works, and how it’s different from other type systems you might be familiar with.

  • Enabling Static Checks

    Learn how to get more power out of Sorbet by enabling static checks.

  • Enabling Runtime Checks

    How to how to get more confidence out of Sorbet by enabling runtime checks.

Adopting Sorbet →
  • What's next?

Get started · Docs · Try · Community · Blog · Twitter