Skip to content

Async function not type checking correctly #15830

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

Closed
OliverJAsh opened this issue May 14, 2017 · 6 comments
Closed

Async function not type checking correctly #15830

OliverJAsh opened this issue May 14, 2017 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@OliverJAsh
Copy link
Contributor

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();
}
@fis-cz
Copy link

fis-cz commented May 14, 2017

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?

@OliverJAsh
Copy link
Contributor Author

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

@fis-cz
Copy link

fis-cz commented May 14, 2017

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)

@OliverJAsh
Copy link
Contributor Author

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

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 15, 2017
@RyanCavanaugh
Copy link
Member

Addressed by #15104

@mhegazy
Copy link
Contributor

mhegazy commented May 30, 2017

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

@mhegazy mhegazy closed this as completed May 30, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants