Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot implement generic type guarding function #18202

Open
apr-xaml opened this issue Sep 1, 2017 · 2 comments
Open

Cannot implement generic type guarding function #18202

apr-xaml opened this issue Sep 1, 2017 · 2 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@apr-xaml
Copy link

apr-xaml commented Sep 1, 2017

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'
@apr-xaml apr-xaml changed the title Breaking change Cannot implement generic type guarding function Sep 1, 2017
@mhegazy
Copy link
Contributor

mhegazy commented 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 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.

@mhegazy mhegazy added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Sep 5, 2017
@mhegazy
Copy link
Contributor

mhegazy commented 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.

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus and removed Design Limitation Constraints of the existing architecture prevent this from being fixed labels Sep 11, 2017
@mhegazy mhegazy added Bug A bug in TypeScript and removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Sep 19, 2017
@mhegazy mhegazy added this to the TypeScript 2.6 milestone Sep 19, 2017
@mhegazy mhegazy modified the milestones: TypeScript 2.7, TypeScript 2.8 Jan 9, 2018
@mhegazy mhegazy modified the milestones: TypeScript 2.8, TypeScript 2.9 Mar 9, 2018
@mhegazy mhegazy modified the milestones: TypeScript 3.0, Future Jul 2, 2018
@sandersn sandersn removed their assignment Jan 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants