Skip to content

Conditional Type is assignable to a discriminated union, yet not working with type guards as expected. #23985

Closed
@DavidKDeutsch

Description

@DavidKDeutsch

(Apologies if this is a duplicate; I suspect that it is after seeing so many related questions being marked "behaving as designed," but am having a hard time figuring out exactly which issue this may be duplicating).

TypeScript Version: 2.8.3

Search Terms: discriminated union, conditional generic, etc...

Code

interface Foo {
    hasADate: true;
    obj: Date;
}

interface Bar {
    hasADate: false;
    obj: number;
}

type FooBar = Foo | Bar;
type GenericFooBar<T> = T extends Function ? Foo : Bar;

function testing<T>(doesntWork: GenericFooBar<T>) {
    if (doesntWork.hasADate === true) {
        return doesntWork.obj.getDate(); // ERROR: arg.obj is still type number | Date
    }

    let works: FooBar = doesntWork;
    if (works.hasADate === true) {
        return works.obj.getDate();     // WORKS: arg.obj is type Date
    }
}

Expected behavior:
Inside of the doesntWork.hasADate === true type guard, doesntWork.obj should be narrowed to a Date

Actual behavior:
doesntWork.obj is still a number | Date. The compiler does allow me to assign doesntWork to works (whose type is the respective discriminated union), and with that variable everything works as expected. It seems odd that the compiler recognizes that GenericFooBar<T> extends Foo | Bar (as it allows the assignment), yet doesn't work with the discriminated type guard.

Activity

added
SuggestionAn idea for TypeScript
and removed
SuggestionAn idea for TypeScript
on May 8, 2018
mhegazy

mhegazy commented on May 8, 2018

@mhegazy
Contributor

This is no different from:

function testing<T extends Foo | Bar>(doesntWork: T){
    if (doesntWork.hasADate === true) {
        return doesntWork.obj.getDate(); // ERROR: arg.obj is still type number | Date
    }
}

The underlying issue here is narrowing does not work on higher order types. what we need here is to first enable narrowing to work on T and in the true branch to be T extends Foo and in the false branch T extends Bar.

#22348 is the start of this change.

ahejlsberg

ahejlsberg commented on May 8, 2018

@ahejlsberg
Member

Adding to what @mhegazy just said... Typically, conditional types are useful in return types where they can depend on type parameters that in turn are used in parameter types such that inferences can be made for them. As you have it written in your example, we can't make any inferences for T and therefore you'd have to always specify it manually--which sort of defeats the purpose.

DavidKDeutsch

DavidKDeutsch commented on May 9, 2018

@DavidKDeutsch
Author

@mhegazy, thanks for that code snippet; I was so sure this had to do with conditional types that I never thought to try a simpler case like that one.

KSXGitHub

KSXGitHub commented on May 15, 2018

@KSXGitHub
Contributor

@DavidKDeutsch Why did you closed this issue? I think this is a good suggestion.

DavidKDeutsch

DavidKDeutsch commented on May 16, 2018

@DavidKDeutsch
Author

@KSXGitHub I closed it because I had not realized that my particular problem really didn't have anything to do with the new conditional type functionality; it's a pre-existing issue with generics in general (as @mhegazy demonstrated for me), and is already being worked on in #22348. I didn't want to clutter the open issues list with a duplicate (I figured it was a duplicate of something when I first posted it, but didn't know what that "something" was).

That being said, if @mhegazy thinks it is a useful ticket to keep open, he or I can reopen it. I'm just trying to be a good citizen :)

locked and limited conversation to collaborators on Jul 31, 2018
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

    SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @DavidKDeutsch@ahejlsberg@mhegazy@KSXGitHub

        Issue actions

          Conditional Type is assignable to a discriminated union, yet not working with type guards as expected. · Issue #23985 · microsoft/TypeScript