Skip to content

Returned Enum value has its type needlessly widened #20254

Closed
@samijaber

Description

@samijaber

TypeScript Version: 2.7.0-dev.20171124

Code

enum Item {
  potato = "potato",
  cucumber = "cucumber"
}

// inferred type is `() => Item`
const getPotato1 = () => Item.potato;

// type assertion works
const getPotato2: () => Item.potato = () => Item.potato;

Expected behavior:
Type of getPotato1 should be inferred as () => Item.potato.

Actual behavior:
Type of getPotato1 is "needlessly" widened to () => Item.

cc @OliverJAsh

Activity

ahejlsberg

ahejlsberg commented on Nov 25, 2017

@ahejlsberg
Member

This is working as intended. See #10676 (comment).

OliverJAsh

OliverJAsh commented on Nov 25, 2017

@OliverJAsh
Contributor

@ahejlsberg

Interesting, thanks for the linking to that explanation!

Widening occurs only if the inferred return type is a singleton literal type.

#10676 (comment)

Here is an example where the return type is not a singleton literal type but a union of objects containing literal types, and the compiler still appears to widen both literal types to their enum union type:

{
    enum Item {
        potato = 'potato',
        cucumber = 'cucumber',
    }

    {
        const get = () => true ? { item: Item.potato, otherVar: true } : { item: Item.cucumber };
        /* Unexpected error:
        Type '{ item: Item; otherVar: boolean; } | { item: Item; }' is not assignable to type '{ item: Item.potato; otherVar: boolean; } | { item: Item.cucumber; }'.
            Type '{ item: Item; otherVar: boolean; }' is not assignable to type '{ item: Item.potato; otherVar: boolean; } | { item: Item.cucumber; }'.
                Type '{ item: Item; otherVar: boolean; }' is not assignable to type '{ item: Item.cucumber; }'.
                Types of property 'item' are incompatible.
                    Type 'Item' is not assignable to type 'Item.cucumber'.
        */
        const result: { item: Item.potato; otherVar: boolean } | { item: Item.cucumber } = get();
        result;
    }
}

Is this also expected behaviour, or a separate issue?

ahejlsberg

ahejlsberg commented on Nov 26, 2017

@ahejlsberg
Member

@OliverJAsh My comment above was regarding literal types as the actual return type. In your example the return type is a union of object types, and the literal types are widened because they are inferred for a mutable location (i.e. a property in an object literal). That is the expected behavior.

OliverJAsh

OliverJAsh commented on Nov 26, 2017

@OliverJAsh
Contributor

Aha, good to know, thanks. Here's an example for anyone else who might be interested:

{
    enum Item {
        potato = 'potato',
        cucumber = 'cucumber',
    }

    {
        const x = { item: Item.potato }
        /* Error:
        Type '{ item: Item; }' is not assignable to type '{ item: Item.potato; }'.
            Types of property 'item' are incompatible.
                Type 'Item' is not assignable to type 'Item.potato'. */
        const result: { item: Item.potato; } = x;
        result;
    }
    // workaround 1: type assertion on property
    {
        const x = { item: Item.potato as Item.potato }
        const result: { item: Item.potato; } = x;
        result;
    }
    // workaround 2: avoid inference
    {
        const x: { item: Item.potato; } = { item: Item.potato }
        const result: { item: Item.potato; } = x;
        result;
    }
}
OliverJAsh

OliverJAsh commented on Nov 26, 2017

@OliverJAsh
Contributor

Also for anyone else who is interested, here is some discussion about widening in object literals and a proposal to change this #20195

typescript-bot

typescript-bot commented on Dec 11, 2017

@typescript-bot
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

locked and limited conversation to collaborators on Jun 14, 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

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @OliverJAsh@samijaber@ahejlsberg@typescript-bot

        Issue actions

          Returned Enum value has its type needlessly widened · Issue #20254 · microsoft/TypeScript