Skip to content

[] is regard wrong as never[] when strictNullChecks is true #13140

Closed

Description

@k8w

TypeScript Version: 2.1.1 / nightly (2.2.0-dev.201xxxxx)
2.1.4

Code

test.ts

function test(obj): any[]{
    let output = [];
    for(let k in obj){
        output.push(obj[k]);
    }
    return output;
}

tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "strictNullChecks": true,
        "noImplicitAny": false,
        "sourceMap": false
    }
}

Expected behavior:
Compile successfully.

Actual behavior:

test.ts(4,21): error TS2345: Argument of type 'any' is not assignable to parameter of type 'never'.

Maybe output should be treated as any[] ?

It would compile successfully if strictNullChecks: false

Activity

  1. mhegazy commented on Dec 26, 2016

    @mhegazy
    Contributor

    An empty array without a type annotation can not be inferred to have any type. If --strictNullChecks is off, it is inferred to be undefined[] and then widened to any[]. if --strictNullChecks, undefined is not in the domain of any type, so it is not inferred. so you are left with a never[].

    Add a type annotation or an intializer to avoid getting a never[].

  2. DanielRosenwasser commented on Dec 26, 2016

    @DanielRosenwasser
    Member

    Why can't we use control-flow analysis so that users get the "smart" behavior that they get with --noImplicitAny?

  3. mhegazy commented on Dec 26, 2016

    @mhegazy
    Contributor

    Why can't we use control-flow analysis so that users get the "smart" behavior that they get with --noImplicitAny?

    cause noImplicitAny is set to false in the config file above. if it is set to true, control flow analysis gets the type of output as any[] since obj is any. you will get an error on obj however, since it is implicitly any.

  4. DanielRosenwasser commented on Dec 27, 2016

    @DanielRosenwasser
    Member

    What I mean is: can we change the behavior so that users get similar behavior as if they did enable noImplicitAny when they enable strictNullChecks?

  5. mhegazy commented on Dec 28, 2016

    @mhegazy
    Contributor

    That was discussed in #12359. and the conclusion was not to do that.

  6. DanielRosenwasser commented on Dec 28, 2016

    @DanielRosenwasser
    Member

    Since I don't see it recorded in the meeting notes, do you recall the justification?

    My understanding is that the reason we didn't do it outside of noImplicitAny alone is that it was a breaking change - but if we infer never[] under strictNullChecks, I don't see how this would possibly be a breaking change for any reasonable cases.

  7. mhegazy commented on Dec 28, 2016

    @mhegazy
    Contributor

    The two bugs listed were the reason. Being a breaking change and causing perf regressions.

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

      Issue actions