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
{{ message }}
Repository navigation
Cannot implement generic type guarding function #18202
When comparing two generic signatures, the source is instantiated in the context of the target then both are compared (see #16368 for more details). since the generic type parameter T is never used in your signature in an input position, so it will not be instantiated, and as a result when comparing the two signatures, T from the first is not the same T from the second, and the comparison will fail, since the compiler can not make any assumptions about the relationship of the two Ts.
The core of the issue here is that is equivalent to casting. i would argue, you are better off casting and avoiding the runtime cost of a function that can not verify the type anyways.
Alternatively, just use --noStrictGenericChecks to disable checking the generic signatures.
The other reason this does not work is that type guards are not an inference position. now that we are inferring from return type position, seems like we should do this too.
Activity
[-]Breaking change[/-][+]Cannot implement generic type guarding function[/+]mhegazy commentedon Sep 5, 2017
When comparing two generic signatures, the source is instantiated in the context of the target then both are compared (see #16368 for more details). since the generic type parameter
Tis never used in your signature in an input position, so it will not be instantiated, and as a result when comparing the two signatures,Tfrom the first is not the sameTfrom the second, and the comparison will fail, since the compiler can not make any assumptions about the relationship of the twoTs.The core of the issue here is that is equivalent to casting. i would argue, you are better off casting and avoiding the runtime cost of a function that can not verify the type anyways.
Alternatively, just use
--noStrictGenericChecksto disable checking the generic signatures.mhegazy commentedon Sep 11, 2017
The other reason this does not work is that type guards are not an inference position. now that we are inferring from return type position, seems like we should do this too.
RyanCavanaugh commentedon Jul 23, 2025
No longer errors