Skip to content

Async function not type checking correctly #15830

Description

TypeScript Version: 2.3.2

Code

{
    const fn = (): Promise<number> => {
        const innerFn = async () => {
            if (1 === 1) {
                return Promise.resolve('wrong type');
            } else {
                return Promise.resolve(1);
            }
        };

        // should error with `Type 'string' is not assignable to type 'number'.`
        return innerFn();
    };

    fn();
}

Expected behavior:
return innerFn() should error with Type 'string' is not assignable to type 'number'.

Actual behavior:
return innerFn() does not error.

If you remove the async keyword from the async function, the program type checks as expected:

{
    const fn = (): Promise<number> => {
        const innerFn = () => {
            if (1 === 1) {
                return Promise.resolve('wrong type');
            } else {
                return Promise.resolve(1);
            }
        };

        return innerFn();
    };

    fn();
}

Activity

  1. fis-cz commented on May 14, 2017

    @fis-cz

    Hi,

    As your inner function does not specify the type the type is recognized automatically form inside of the function - I just checked:

    with async its

    Promise<string|number>
    

    without async its

    Promise<string> | Promise<number>
    

    Isn't this correct behavior?

  2. OliverJAsh commented on May 14, 2017

    @OliverJAsh
    ContributorAuthor

    Hi Fis (@fis-cz). The inner type is inferred to have that return type, however the outer fn has a return type of Promise<number>, which Promise<string|number> is not assignable to.

  3. fis-cz commented on May 14, 2017

    @fis-cz

    when I do

    {
        const fn = (): Promise<number> => {
            const innerFn = async (): Promise<number> => {
                if (1 === 1) {
                    return Promise.resolve('wrong type');
                } else {
                    return Promise.resolve(1);
                }
            };
    
            // should error with `Type 'string' is not assignable to type 'number'.`
            return innerFn();
        };
    
        fn();
    }
    

    it works correctly. Is there a reason why not to explicitly define return type of the innerFn? (ofc, it should be working correctly in both cases)

  4. OliverJAsh commented on May 14, 2017

    @OliverJAsh
    ContributorAuthor

    I don't always add type annotations for my function returns. I don't want it to silently fail when relying on inference.

  5. RyanCavanaugh commented on May 15, 2017

    @RyanCavanaugh
    Member

    Addressed by #15104

  6. mhegazy commented on May 30, 2017

    @mhegazy
    Contributor

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

  7. 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

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions