Skip to content

Type guard fails to narrow type with generic parameters #1943

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

Closed
hesselink opened this issue Feb 5, 2015 · 1 comment
Closed

Type guard fails to narrow type with generic parameters #1943

hesselink opened this issue Feb 5, 2015 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@hesselink
Copy link

The following code type checks:

class C
{
    foo: number;
}
function foo (x : C | Array<C>) : C
{
    if (x instanceof C)
      return x;
    return x[0];
}

However, if we add a type parameter to C, the following fails:

class C<A>
{
    foo: A;
}
function foo<A> (x : C<A> | Array<C<A>>) : C<A>
{
    if (x instanceof C)
      return x;
    return x[0];
}

There is an error on line 8 (return x;):

Type 'C<A> | C<A>[]' is not assignable to type 'C<A>'. Type 'C<A>[]' is not assignable to type 'C<A>'. Property 'foo' is missing in type 'C<A>[]'.

I've tried a couple of other things, but can't get this to type check. Is this a bug, or am I missing an obvious solution?

@mhegazy
Copy link
Contributor

mhegazy commented Mar 24, 2015

looks like the same issue in #2388

@mhegazy mhegazy closed this as completed Mar 24, 2015
@mhegazy mhegazy added the Duplicate An existing issue was already created label Mar 24, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants