Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
See https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#intersection-types. This is only on occurrence as an example, the pattern repeats in many places in the type definitions
Using
&
might be convenient while defining types, but leads to ugly situations when using theses types. With Union Types(|
) TypeScript would correctly infer the actual type by using a simpleif
that checks for a specific property or by using a type guard. But like in the given example, everything is merged together and there is no way to differentiate between aentity
orcontext
etc. Which is forcing the consumer to do a type cast. The existingNOTE
is actually correct, in many situations we end up withnever
which is not really helpfulIn other place there is code like
string & { id:string, ...}
. I think the intend is not to say that it is a string with additional properties, most of the time it means OR. So usingif(typeof myVar === "string"){...}
would do the job for a union type. In the if block TypeScript would then treatmyVar
only as string and the further implementation would be pretty clean.I would like to use this PR as a starting point for the discussion, doing the actual change might have a bigger impact.