Allow calling union types with different call signatures when arguments are unions of types with at least one type satisfying one of the functions and a type guard narrows the argument union to the correct type at runtime #37223
Labels
Duplicate
An existing issue was already created
Search Terms
calling union types
Suggestion
Give TS the ability to infer that calling a value that is typed with a union of functions of differing call signatures is valid when a type guard that narrows the union of functions to a function, or a new union of functions with identical signatures, is used in a conditional expression to select arguments with the correct type to be passed.
Use Cases
Creating a TypedArray using a variable TypedArrayConstructor:
Examples
This does not work as of TS 3.7.5:
In the 1st and 2nd case,
ZERO
should be narrowed down to the appropriate type as the compiler should be able to infer thatdoesTakeNumbers
is pure and thus definingZERO
with a conditional expression at the top level is equivalent to definingZERO
inside both theif
andelse
blocks with the appropriate0
for each respectively.In the 3rd case, the compiler should be able to infer that
ZERO
will be the correct type for use as the argument tox
during runtime sinceZERO
should be conditionally typed correctly based on the result of the type guarddoesTakeNumbers
called withx
. Maybe this could be done with an extension to the existing conditional types where the compiler could infer the type ofZERO
to be a conditional type instead of abigint | number
,Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: