Skip to content

"return null" within "for loop" within async method does not respect strictNullChecks #14298

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
willportnoy opened this issue Feb 24, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@willportnoy
Copy link
Member

willportnoy commented Feb 24, 2017

TypeScript Version: 2.2.1

strictNullChecks set to true

Code

type Func_Async<R> = () => Promise<R>;

// no errors here, expected error like below examples
const fail_NoNullError: Func_Async<Object> =
   async () => {
        for (let index = 0; index < 1; ++index) {
            return null;
        }

        return {};
    };

// as expected
// error TS2322: Type '() => Promise<null>' is not assignable to type 'Func_Async<Object>
const good_HasNullError: Func_Async<Object> =
   async () => {
        return null;
    };

type Func_Sync<R> = () => R;

// as expected:
// error TS2322: Type '() => {} | null' is not assignable to type 'Func_Sync<Object>'.
const good_HasNullErrorSync: Func_Sync<Object> =
    () => {
        for (let index = 0; index < 1; ++index) {
            return null;
        }

        return {};
    };

Expected behavior:

I expected "return null" in the middle of a for loop within an async method should produce a compiler error with strictNullChecks, much like the other sync and async examples above.

Actual behavior:

The code compiles without any errors.

@aluanhaddad
Copy link
Contributor

I think the issue may stem from the type signature of .then

@mhegazy
Copy link
Contributor

mhegazy commented Feb 27, 2017

This is stems from the same issue detailed in #13970

@mhegazy mhegazy added the Duplicate An existing issue was already created label Feb 27, 2017
@mhegazy mhegazy closed this as completed Apr 21, 2017
@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants