Correction
Upon review, I realized there was an error in my initial response where I misinterpreted type B = A | {...}
as opposed to type B = A & {...}
After taking this into account, the most accurate answer comes from the comment made by @jcalz in response to your question. It clarifies that &
actually signifies an intersection, and that there are no shared characteristics between type A
and
{ type: 'something'; record: string; }
, resulting in the type
never
, as demonstrated in the playground's intellisense.
If you still wish to pursue the original concept I mentioned, I have retained the initial response below.
Original response (type B = A | {...}
)
The issue lies in the fact that the second and third assignments do not align with the definition of B
, as expected due to the functionality of the |
operator. In this scenario, type B can only feature the property "type" with potential values 'error' and 'operator' (represented by type A
), or the property "type" with a fixed value of 'something' in addition to a "record" property. It is not possible to blend these types.
To resolve this, you need to redefine B as follows:
type B = {
type: A["type"] | 'something';
record: string;
}
It's important to note that this adjustment requires you to always provide a value for the "record" property. As a result, the following assignment will no longer be valid:
b = {
type: 'error'
}
If you also intend to support the plain A type, you must reintroduce the |
operator:
type B = A | {
type: A["type"] | 'something';
record: string;
}