It may seem odd at first glance, as the type in question is essentially a string
.
However, if you were to specify it as
'alert' | 'tree' | 'treegrid' | 'treeitem' | string
, then your type would simply be reduced to just
string
. This would result in limited autocomplete suggestions when writing code like this:
let role: AriaRole = 'a
In this case, TypeScript would only recognize AriaRole
as a basic string
, providing generic autocomplete options without knowledge of the specific values. On the other hand, using string & {}
prevents the simplification to string
, allowing for enhanced autocomplete suggestions such as 'alert'
or 'tree'
, while still permitting any other string input.
Ultimately, structuring the type in this manner appears to encourage certain strings through autocomplete suggestions, while also making these preferred strings visible upon hovering over the type name.