Why does Type Resolution fail when using SELECT DISTINCT instead of SELECT on PostgreSQL?
Without the DISTINCT, the type of the second part of the UNION ALL is unknown:
SELECT pg_typeof(NULL);

 pg_typeof 
-----------
 unknown
(1 row)

This gets resolved to integer.
If there is a DISTINCT, PostgreSQL has to resolve the type of NULL earlier on: to eliminate duplicates, it has to know which equality operator to use. Lacking other information, it chooses text as the preferred type of the string category.
Then you get the error because integer and text cannot be reconciled.
The documentation describes the type choice for the equality operator as follows:

If any input arguments are unknown, check the type categories accepted at those argument positions by the remaining candidates. At each position, select the string category if any candidate accepts that category. (This bias towards string is appropriate since an unknown-type literal looks like a string.)