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

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

Closed
DanielRosenwasser opened this issue Jan 12, 2016 · 3 comments
Closed
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@DanielRosenwasser
Copy link
Member

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.

@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Jan 12, 2016
@mhegazy mhegazy added this to the TypeScript 2.0 milestone Jan 13, 2016
@sandersn
Copy link
Member

sandersn commented Mar 7, 2016

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.

@weswigham, do you have an idea of the right thing to do here?

@mhegazy
Copy link
Contributor

mhegazy commented Apr 21, 2016

this is handled by #8010

@ahejlsberg
Copy link
Member

Fixed in #8010.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Apr 22, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants