Skip to content

Confusing 'never' return type when appending an empty array #9976

Closed
@rtpg

Description

@rtpg

TypeScript Version: 2.0.0

Code

        var x = [];
        x.push(1);

Expected behavior:
Accept this, and have the type inferencer postulate x:number[]. Alternatively, an error message along the lines of "cannot assign number to parameter {}" (because type inferencer could also assume x:undefined[]).

Actual behavior:
(with strictNullCheck)

error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.

This might be because of how never is documented as the "bottom" type (for functions that never return), but since the code isn't using the result expression, this is pretty confusing. Is this because the assumed type of x is never[]?

I tried looking for related issues, I feel like it's slightly related to empty tuple discussions, but not 100% sure.

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