You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Union types are reduced to the smallest possible set of constituent types using these rules
But in compiler they are not reduced in case of inferring type parameters in function invocations. For example:
varo={};// According to spec, type of p should be reduced to {}functionf<T>(p: T|typeofo): T{vart: T;vartemp=t||o;// here union type is reduced, and it is really {}returnnull;}// Another function where type of p is really {}. According to spec, it should be equal to f, shouldn't it?functionf2<T>(p: typeofo): T{returnnull;}// But really they are differentvare=f(5);// numbervare2=f2(5);// {}
Could you please clarify, when union type are really reduced and when they are not.
The text was updated successfully, but these errors were encountered:
I imagine you are using the latest from master. Constituents in a union type are no longer reduced, at least not by the subtype relation. I believe this change is reflected in spec update #4033.
Union types are reduced to the smallest possible set of constituent types using these rules
That paragraph has been removed from the spec.
I have yet to add a description of the new deduplication algorithm, but basically a type S is considered a duplicate of a type T if:
S and T are identical,
S is undefined,
S is null and T is not undefined,
S is an object literal type, T is an object type, and S has a property set that is a duplicate of T's,
S and T are array types, and S has an element type that is a duplicate of T's.
S and T are tuple types, and S has element types that are duplicates of T's.
In other words, we get rid of types that are identical to other types in the set, except they may have nulls or undefineds where the other types don't.
The implementation of this algorithm is in the removeDuplicateTypes function in checker.ts.
Spec 3.4 says:
But in compiler they are not reduced in case of inferring type parameters in function invocations. For example:
Could you please clarify, when union type are really reduced and when they are not.
The text was updated successfully, but these errors were encountered: