steep icon indicating copy to clipboard operation
steep copied to clipboard

Context typing with some special methods like `#freeze`

Open soutaro opened this issue 3 years ago • 2 comments

Problem

We often write code using #freeze or #-@.

# @type const DEFAULT_KEY: "user_id"
DEFAULT_KEY = "user_id".freeze

This doesn't work because literal type cannot be given to non-literal expressions.

Proposal

Let Steep to infer literal types on a method call when the following conditions hold:

  1. The method returns self
  2. The receiver can have literal type

The rule No. 1 would be the key. It means that the method says the type of the return value is exactly same with the type of the receiver.

soutaro avatar Mar 28 '21 16:03 soutaro

Ran into this issue with a frozen record. Took a while to figure out where the error was coming from.

rainhead avatar Mar 04 '22 18:03 rainhead

Cannot be done in Steep 1.5.

soutaro avatar Jul 12 '23 13:07 soutaro