Cannot implement generic type guarding function #18202
Description
Activity
- changed the title
[-]Breaking change[/-][+]Cannot implement generic type guarding function[/+]on Sep 1, 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.- addedDesign LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
on Sep 5, 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.
- addedSuggestionAn idea for TypeScriptAn idea for TypeScriptIn DiscussionNot yet reached consensusNot yet reached consensusand removedDesign LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
on Sep 11, 2017 - addedBugA bug in TypeScriptA bug in TypeScriptand removedIn DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript
on Sep 19, 2017 RyanCavanaugh commented
on Jul 23, 2025 MemberMore actionsNo longer errors
- locked as resolved and limited conversation to collaborators
on Jan 20, 2026
TypeScript Version: 2.5.2
Code
Expected behavior:
Class Checker should be treated as a valid implementation (used to be in Ts-2.3).
Actual behavior:
Compiler complains:
(...)