Skip to content

Cannot implement generic type guarding function #18202

Description

@apr-xaml

TypeScript Version: 2.5.2

Code

interface ITypeChecker
{
    isOfType<T extends TBase, TBase>(obj: TBase): obj is T; 
}


class Checker implements ITypeChecker
{
    isOfType<T extends TBase, TBase>(obj: TBase): obj is T
    {
        return true;
    }

}

Expected behavior:
Class Checker should be treated as a valid implementation (used to be in Ts-2.3).
Actual behavior:
Compiler complains:

  • interface-not-properly-implemented
    (...)
    • Type predicate 'value is TBase' is not assignable to 'value is T'
      • Type 'TBase' is not assignable to type 'T'

Activity

  1. changed the title [-]Breaking change[/-] [+]Cannot implement generic type guarding function[/+] on Sep 1, 2017
  2. mhegazy commented on Sep 5, 2017

    @mhegazy
    Contributor

    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.

  3. mhegazy commented on Sep 11, 2017

    @mhegazy
    Contributor

    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.

  4. added
    SuggestionAn idea for TypeScript
    and removed
    Design LimitationConstraints of the existing architecture prevent this from being fixed
    on Sep 11, 2017
  5. RyanCavanaugh commented on Jul 23, 2025

    @RyanCavanaugh
    Member

    No longer errors

  6. locked as resolved and limited conversation to collaborators on Jan 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions