0.0
The project is in a healthy, maintained state
Ports TypeScript's non-null assertion `!` to Ruby as a method, inspired by Crystal's `not_nil!`. The bundled RBS signatures let Steep narrow `T?` to `T` at the type level.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

NotNilable

A non-null assertion operator for Ruby. Adds Object#not_nil!, ported from TypeScript's ! operator and inspired by Crystal's not_nil!.

When the receiver is anything but nil, .not_nil! returns the receiver unchanged. When it is nil, the method raises NotNilable::NilAssertionError. The bundled RBS signatures let Steep narrow T? to T at the type level.

Installation

bundle add not_nilable

Usage

require "not_nilable"

"hello".not_nil!   # => "hello"
42.not_nil!        # => 42

nil.not_nil!
# raises NotNilable::NilAssertionError: Nil assertion failed

nil.not_nil!("user must be present")
# raises NotNilable::NilAssertionError: user must be present

With RBS + Steep, the type is narrowed:

val = ENV.fetch("HOME", nil)   # type: String?
val.not_nil!.length            # narrowed to String — .length is OK

Development

After cloning the repo, run bundle install, then bundle exec rake to lint, run specs, and type-check.

License

MIT.