Skip to content

How to extract nullable type  #3623

@lud

Description

@lud

Hi,

I would like to build this function:

function bang($value)
{
    if (null === $value) throw ...
   return $value;
}

And I would call it like this:

class A {
    public function maybeString(): ?string
    {
        if (...) return null;
        else return 'hello';
    }
    
    public function maybeThis(): ?self
    {
        if (...) return null;
        else return $this;
    }
}

bang($a->maybeString()); // @return string
bang($a->maybeThis()); // @return A

So I would like to write a typespec for the bang function, i.e. transforming a type ?T into T.

Is it possible to write such a typespec ?

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions