Skip to content

Narrowing does not work for variables typed with type parameters #6445

Description

interface Base { b }
interface Derived extends Base { d }

declare function isDerived(x: Base): x is Derived;

function f<T extends Base>(x: T, y: T) {
    if (isDerived(x) && isDerived(y)) {
        return x.d === y.d;
    }

    return false;
}

Currently, this errors because both x and y still have type T, so they are missing the d property.

Activity

  1. sandersn commented on Mar 7, 2016

    @sandersn
    Member

    getNarrowedType doesn't check whether the type it's narrowing is a type parameter with a constraint. It just calls isTypeAssignableTo(Derived, T), which is false.

    getNarrowedType needs to instead check isTypeAssignableTo(Derived, Base), but I'm not sure whether it's appropriate to narrow to Derived at that point, or to T extends Derived. Probably the latter.

    Wesley Wigham (@weswigham), do you have an idea of the right thing to do here?

  2. mhegazy commented on Apr 21, 2016

    @mhegazy
    Contributor

    this is handled by #8010

  3. ahejlsberg commented on Apr 22, 2016

    @ahejlsberg
    Member

    Fixed in #8010.

  4. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions