Skip to content

Commit

Permalink
Merge pull request #8493 from HypeMC/object-properties
Browse files Browse the repository at this point in the history
Document the object with properties syntax
  • Loading branch information
orklah committed Sep 18, 2022
2 parents 3724a83 + 014cddb commit 1f1fc47
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/annotating_code/type_syntax/atomic_types.md
Expand Up @@ -21,6 +21,7 @@ Atomic types are the basic building block of all type information used in Psalm.
## [Object types](object_types.md)

- [object](object_types.md)
- [object{foo: string}](object_types.md)
- [Exception, Foo\MyClass and Foo\MyClass<Bar>](object_types.md)
- [Generator](object_types.md)

Expand Down
19 changes: 19 additions & 0 deletions docs/annotating_code/type_syntax/object_types.md
Expand Up @@ -2,6 +2,25 @@

`object`, `stdClass`, `Foo`, `Bar\Baz` etc. are examples of object types. These types are also valid types in PHP.

#### Object properties

Psalm supports specifying the properties of an object and their expected types, e.g.:

```php
/** @param object{foo: string} $obj */
function takesObject(object $obj) : string {
return $obj->foo;
}

takesObject((object) ["foo" => "hello"]);
```

Optional properties can be denoted by a trailing `?`, e.g.:

```php
/** @param object{optional?: string} */
```

#### Generic object types

Psalm supports using generic object types like `ArrayObject<int, string>`. Any generic object should be typehinted with appropriate [`@template` tags](../templated_annotations.md).
Expand Down

0 comments on commit 1f1fc47

Please sign in to comment.