How to instantiate a shape type object in hacklang

393 views Asked by At

This seems like it should be in the documentation, but it isn't.

I have a shape type.

type ThisIsMyShapeType = shape(
  'some_prop' => bool,
);

How do I instantiate an object of this type ?

1

There are 1 answers

0
Josh Watzman On BEST ANSWER

You use the same shape keyword:

$x = shape(
  'some_prop' => true,
);

In Hack, shapes use structural typing so you don't need to declare your variable is a ThisIsMyShapeType -- the typechecker will verify that $x has all of the right fields to match the type.

There are quite a few examples in the official documentation.