Add a "autocomplete" field in a buildForm How do I add an autocomplete field in a class that extends FormBase?

I created a custom module, which uses a class that extends FormBase. I want to  programmatically create a form field that autocompletes the username from the user registered in the site.
You use entity_autocomplete as #type for the form field. CommentForm::form() contains such a form field, for the administrator users to be able to change the author of a comment. This is the code it uses.

  // The uid field is only displayed when a user with the permission
  // 'administer comments' is editing an existing comment from an
  // authenticated user.
  $owner = $comment-&gt;getOwner();
  $form['author']['uid'] = [
    '#type' =&gt; 'entity_autocomplete',
    '#target_type' =&gt; 'user',
    '#default_value' =&gt; $owner-&gt;isAnonymous() ? NULL : $owner,
    // A comment can be made anonymous by leaving this field empty therefore
    // there is no need to list them in the autocomplete.
    '#selection_settings' =&gt; ['include_anonymous' =&gt; FALSE],
    '#title' =&gt; $this-&gt;t('Authored by'),
    '#description' =&gt; $this-&gt;t('Leave blank for %anonymous.', ['%anonymous' =&gt; $config-&gt;get('anonymous')]),
    '#access' =&gt; $is_admin,
  ];


As you can see from the code, you can also choose to include the anonymous user.

The result is shown in the following screenshot.