Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document the object with properties syntax #8493

Merged
merged 1 commit into from Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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