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
typeA={type: 'a',a: number}typeB={type: 'b',b: string}typeUnion=A|Bconstarr: Union[]=[{type: 'a',a: 10},{type: 'a',a: 20},{type: 'b',b: '30'}]// It should now have been refined as an A[], but is notconstfiltered: A[]=arr.filter(x=>x.type==='a')
Expected behavior:
Just like the compiler can infer that the code under a branch following an if (x.type === 'a') can safely use x as an A, it could be able to do it when using an anonymous function in Array.prototype.filter
Actual behavior:
Instead, one must first define a function :
constisA=(u: Union): u is A=>u.type==='a'
and use it to filter the Array. Note that the body of the function is exactly the same as the anonymous function's above, all it does is giving an extra hint to the compiler by way of the return type. Thus, the anonymous function could be automatically picked up to use the proper Array.prototype.filter definition.
Creating lots of isX boilerplate functions is known to be a tedious task for lots of big unions, it would be nice if it wasn't mandatory.
The text was updated successfully, but these errors were encountered:
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Code
Expected behavior:
Just like the compiler can infer that the code under a branch following an
if (x.type === 'a')
can safely usex
as anA
, it could be able to do it when using an anonymous function inArray.prototype.filter
Actual behavior:
Instead, one must first define a function :
and use it to filter the Array. Note that the body of the function is exactly the same as the anonymous function's above, all it does is giving an extra hint to the compiler by way of the return type. Thus, the anonymous function could be automatically picked up to use the proper Array.prototype.filter definition.
Creating lots of
isX
boilerplate functions is known to be a tedious task for lots of big unions, it would be nice if it wasn't mandatory.The text was updated successfully, but these errors were encountered: